user3839908
user3839908

Reputation: 35

Concerning pointer and reference in class

Here's my program

class Base
{
public:
  void fc()
  {
    cout << "Class function is called" << endl;
  }
};

void main()
{
  Base* ptr;
  ptr = 0;
  Base b1, b2;
  *ptr = b1;
  ptr->fc();
  ptr = 0;
  ptr = &b2;
  ptr->fc();
}

It runs OK My question is simple:

You can assign object to pointer by *ptr=b1; or ptr=&b1; so what's the difference between these two, if any? Can you go deeper as why there are two ways of assigning pointer if they achieve the same goal? Are they fundamentally different?

Secondly, some people call '&' reference sign, some say it's 'get address of' can some one clear this out for me ? More over what's the difference between reference and pointer?

Thanks a lot

Linus

Upvotes: 0

Views: 69

Answers (3)

tomdzon
tomdzon

Reputation: 91

@balajeerc you'll have memory leak that way... You're pointing to object objB now, and memory that's reserved earlier can't be accessed anymore nor freed.

Upvotes: 0

balajeerc
balajeerc

Reputation: 4098

When you do *ptr = b1, what you are doing is as follows: 1) Dereferencing the object that is already at the memory location pointed to be ptr 2) Copying over the members of b1 to the object deferenced in 1

Which is why, your code will give you a segmentation fault if you utilised a member variable. Consider the following modification for your class:

#include <iostream>
#include <string>

class Base
{
public:
    Base():name("undefined")
    {
    }

    Base& operator=(const Base& other){
        std::cout << "Copying from " << other.name << " to " << name << std::endl;
        name = other.name;
        return *this;
    }

    void fc(){
        cout<<"Class function is called"<<endl;
    }

    std::string name;
};

Now, with the above modifications, the following code will segfault:

int main()
{   Base *ptr; ptr=0;
    Base b1,b2;
    *ptr=b1; // will segfault here
    ptr->fc();
    ptr=0;
    ptr=&b2;
    ptr->fc();
    return 0;
}

In the second case, i.e. when you call

ptr=&b2;

what you are doing is merely setting the address of the memory location that ptr points to. So, the following code WILL run fine:

int main()
{   Base *ptr; ptr=0;
    Base b1,b2;
    ptr=&b2;
    ptr->fc();
    return 0;
}

A more illustrative example will be the following:

int main()
{   Base *ptr;

    Base objA;
    objA.name = "objA";
    Base objB;
    objB.name = "objB";

    ptr = new Base();
    ptr->name = "ptrObj";
    *ptr = objA;
    std::cout << "Ptr is now " << ptr->name << std::endl;

    ptr = &objB;
    std::cout <<  "Ptr is now " << ptr->name << std::endl;    

    return 0;
}

Which gives the following output:

Copying from objA to ptrObj
Ptr is now objA 
Ptr is now objB

Upvotes: 1

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

You can assign object to pointer by *ptr=b1; or ptr=&b1; so what's the difference between these two,

First one de-references the ptr and changes the value in the address that ptr points to. Second one changes where ptr points in the memory to the address of b1.But in your example since the ptr doesn't point to a valid address in memory dereferencing it will cause undefined behaviour.

Secondly, some people call '&' reference sign, some say it's 'get address of' can some one clear this out for me ?

It depends on the context.For example when you use it like ptr = &b1 it means address-of operator. When you use it in a declaration it means a reference for ex: int &i; means i is a reference to an int.If you use it in a boolean expression it means AND or possibly Bitwise AND operator.There may be other scenarious that I missed but the important thing is to know that what it means depends on where you are using it.

Upvotes: 0

Related Questions