Jump to content
  • Advertisement
Sign in to follow this  
coordz

C++ reference rather than pointer

This topic is 4264 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say I've got a class myClass then I can write a function like myfunc(myClass &a) and then in the function use the reference to call a member function like a.myMemeberFunc() If I write the function like myfunc(myClass *a) then I have to call member functions like a->myMemberFunc() All well and good. Now, within a function (a member function of another class, myClass2, as it happens) I want to write something along the lines of myClass *refToA = &member where member is a member variable of myClass2 and is of type myClass. From then on in the function I have to use refToA->myMemberFunc() Here's the question: I want to be able to write refToA.myMemberFunc() as I could with the reference approach. How do I write the initial declaration and assignment of refToA so I can do this? TIA

Share this post


Link to post
Share on other sites
Advertisement
maybe you should provide a constructor that takes a "myClass" argument to initialize it:


myClass::myClass(myClass &x)
{
//and provide operator=() overloaded function
this = x;
}

myClass refToA(&member)
refToA.myMemberFunc()


but... probably i'm wrong, well give it a try

Share this post


Link to post
Share on other sites
Quote:
Original post by VerMan
maybe you should provide a constructor that takes a "myClass" argument to initialize it:


myClass::myClass(myClass &x)
{
//and provide operator=() overloaded function
this = x;
}

myClass refToA(&member)
refToA.myMemberFunc()


but... probably i'm wrong, well give it a try


Yes, you're wrong. In many ways.

Firstly, that's called a copy constructor. You should write operator=() in terms of the copy constructor and not the other way around, otherwise you will end up potentially dereferencing uninitialized values, or constructing values and immediately overwriting them with new values.

Zerothly, you should never assign to "this". Bad bad bad.

Minusonethly, your argument x is a reference to a myClass object, and you're trying to assign it to this which is of type pointer-to-myClass.

Nope, ToohrVyk provides the answer.

--smw

Share this post


Link to post
Share on other sites
there is no way to initialize a class member the way ToohrVyk proposes. one should use initialization lists in constructor:


class c1 {};
class c2
{
c1& c1ref;
c1(const c1& ref): c1ref(ref) {}
};

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk

A a1;
A& a2 = a1; // a2 references a1

assert (&a1 == &a2);

a2.member();


Does the job nicely. Thanks a lot.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk

class c2
{
c1& c1ref;
c2(const c1& ref): c1ref(ref) {}
};


why without const? your way you won't be able to construct c2 instance from a temporary object, i.e. you won't be able to:


c1 x, y;
c2 z(x+y); // oops

const c1 fun() {...};
c2 z(fun()); // oops

Share this post


Link to post
Share on other sites
Quote:
Original post by mbaitoff
Quote:
Original post by ToohrVyk

class c2
{
c1& c1ref;
c2(const c1& ref): c1ref(ref) {}
};


why without const? your way you won't be able to construct c2 instance from a temporary object, i.e. you won't be able to:


c1 x, y;
c2 z(x+y); // oops

const c1 fun() {...};
c2 z(fun()); // oops


Without const, because you shouldn't be able to construct c2 instance from a temporary. Think of it, c2 will save the reference of such temporary object in it's c1ref member. Bad things can (and will) happen.

Share this post


Link to post
Share on other sites
Quote:
Original post by mbaitoff
why without const?


  1. The code does not compile with const, because you would be creating a non-constant reference (c2::c1ref) from a constant reference (ref), thus causing a compiler error.
  2. The compiler has no reason to let it compile, since the policy is to make const-conversions implicit through use of const_cast. Besides, it makes things confusing, since you're passing a constant reference that the instance may modify!
  3. Even if c2::c1ref was also constant, creating instances of objects that reference temporary values is a Bad IdeaTM, since the lifetime of the temporary value ends before the object is fully constructed — yielding a member variable that cannot be used except in the constructor.



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!