Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-06-2006, 12:06 AM   #1 (permalink)
deepak_kr_mehta
Guest
 
Posts: n/a
show me diffrence b/w x&,x*

hello all memebers
sir i want to know the full detail of pointer
actually i know c pointer very well but i m confuse in this program
Code:
#include<iostream>
using namespace std;
class ratio
{
int num,den;
public:
ratio(int n=1,int d=0):num(n),den(d){}
ratio(ratio &x)   //showing the garbage value when i use this is it pointer
{
x.num=this->num;
x.den=this->den;
}
//but when i do this ,this way it is showing right
                   /*rato(ration *x)
                        {
                       x->num=this->num;
                       x->den=this->den; 
                       }  */
print()
{
cout<<num<<"/"<<den;
}};
int main()
{
ratio x;
ration y(x);
y.print();
x.print();
}
if i don't know diffrence plz explain me
thanx
from
deepak kr.
  Reply With Quote
Old 03-06-2006, 07:31 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
& is dereferencing, and can't be used in any decleration of a function type, instead you use * to show it is the pointer.
Perhaps a look at the The & and functions description for C will make it more clear for you.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 03-06-2006, 08:19 AM   #3 (permalink)
AssKoala
Anti-Zealot
 
AssKoala's Avatar
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 72
AssKoala is on a distinguished road
Send a message via AIM to AssKoala Send a message via MSN to AssKoala Send a message via Yahoo to AssKoala
Er, & isn't dereference.

& is "address of". C++ adds functionality such that it works to create references when defining your function (i.e. it can act as "reference of").

* is dereference. If you have a pointer, it will give you the value contained at the address that is the pointer's value (e.g. a pointer variable has a value of 0xDEADBEEF if you derefence it, it goes to address 0xDEADBEEF and gives you the value contained there).

The "->" notation is just a shortcut.

The following two lines of code are equivalent:
Code:
this->data = 5;
(*this).data = 5;
Prime example of proper usage is in swap.

C Swap:
Code:
#include <stdio.h>
#include <stdlib.h>

void swap(int* a, int* b) {
        if(a && b) {
                int temp = *a;
                *a = *b;
                *b = temp;
        }
}

int main(void) {
        int a = 5;
        int b = 6;

        swap(&a,&b);

        printf("a: %d\n",a);
        printf("b: %d\n",b);
    
        return EXIT_SUCCESS;
}
C++ Swap:
Code:
#include <iostream>
#include <stdlib.h>

void swap(int &a, int &b) {
        int temp = a;
        a = b;
        b = temp;
}

int main(void) {
        int a = 5;
        int b = 6;

        swap(a,b);

        std::cout << "a: " << a << std::endl;
        std::cout << "b: " << b << std::endl;

        return EXIT_SUCCESS;
}
__________________
If you always think like an expert, you'll always be a beginner. | "A handful of knowledgeable people is more effective than an army of fools" -Writing Secure Code, 2nd Ed.
AssKoala is offline   Reply With Quote
Old 03-06-2006, 12:37 PM   #4 (permalink)
kyoryu
Registered User
 
Join Date: Apr 2003
Posts: 34
kyoryu is on a distinguished road
& in a declaration, in c++, means that the object is passed in as a reference.
Code:
void foo (int &bar)
{
   bar=3;
}
This code defines a function (foo) that takes an int reference (bar) as its only parameter. Since it's a reference, the int variable passed to this function will be set to 3 when the function exits. That is,

Code:
int baz;
foo( baz );
printf( "%d", baz );
will ouput the number 3.

References are basically pointers, except with object semantics. They have most of the advantages of pointers (not making copies, ability to send data back), and the advantages of values (simpler syntax, shouldn't have to check for null) all in one.

& used in a statement (not in a declaration), is the address-of operator, and simply returns the address of the object that it is used with.

Code:
int bar;
printf( "%u", &bar );
This will print the address of the variable bar (if it compiles, I don't know if that code is safe or not and haven't compiled it).

The real problem you're having is in this code:

Code:
ratio(ratio &x)   //showing the garbage value when i use this is it pointer
{
    x.num=this->num;
    x.den=this->den;
}
In class of name 'foo', a method with the name of the class is a constructor. A constructor builds the class, does whatever initialization you want it to do, and then returns your newly constructed object.

In this case, you're not initializing your class (that is, the "this" pointer)... you're changing the values of the passed-in parameter (x). Now, you can do this, but the problem is that you're using uninitialized data (the local members of the ratio being constructed), which is a bad thing!

If you're trying to build your new ratio object, copying the old one, this code would do that.

Code:
ratio(ratio &x) 
{
    num = x.num; // this->num and num are the same thing, since we're
                       // in a member method/constructor
    den = x.den
}
though I'd make x const on principle.

The following code would be a good optimization, however:

Code:
ratio(ratio &x) :   //showing the garbage value when i use this is it pointer
    num( x.num ),
    den( x.den )
{
}
The : following the constructor signature specifies how to initialize the variables in the list following it.

If you're actually wanting to modify the ratio that you're passing, what you need to do is call your base constructor (ratio()) explicitly, as in this code:

Code:
ratio(ratio &x) : 
   ratio()            // explicitly call base constructor to initialize num and den
{
    x.num=this->num;
    x.den=this->den;
}
kyoryu is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 12:30 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting