Jump to content
  • Advertisement
Sign in to follow this  
Puck

[solved] Returning a reference to a new object

This topic is 3412 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

Hello all, I have a bit of dilemma in a little C++ project I'm working on. The code is a bit lengthy, so I'll try to simplify it. I have two classes -- let's call them "A" and "B" -- which I want to allow conversions between that don't require an intermediate variable, say:
somefunc(A.toB());

As opposed to:
B tmp; AtoB(AInstance, tmp); somefunc(tmp);

The obvious solution is to simply return an instance of the class I want to convert to, but because the conversion goes both ways, I have to return a reference since one of the classes has an incomplete type at the time of the method's declaration:
class B;

class A {
	/*ctor, dtor, etc.*/
	B& toB();
}

class B {
	/*ctor, dtor, etc.*/
	A& toA();
}

B& A::toB() {
	return B(*this); /*Overloaded constructor does the conversion*/
}

A& B::toA() {
	return A(*this);
}

But I keep getting told that it's an "invalid initialization of non-const reference." I've googled the error and found that it's apparently because that statement returns a temporary. The problem is that none of the articles or postings I found offered a solution that would work in my case. Oddly enough, the compiler doesn't complain about the following code I found on a site about operator overloading:
A& operator+=(const A& rhs) {
	/*add*/
	return *this;
}

A& operator+(const A& rhs) {
	return A(*this) += rhs;
}

I don't quite understand the difference there, aside from the const-ness of the argument. Maybe someone can enlighten me on the subject. I could possibly return a pointer instead, but I can't rely on "somefunc" to free the pointer, so I would have to use a smart pointer or some similar mechanism, which seems excessive and confusing for such a seemingly simple task. So, with all that said here's my main question: What's the best way to return a reference to a new object in C++? Should I just resign myself to using a smart pointer instead, or is there a clever trick I'm unaware of? (As a side note, I see the forum engine still doesn't like plus signs [wink]) [Edited by - Puck on April 17, 2009 7:54:14 PM]

Share this post


Link to post
Share on other sites
Advertisement
You can declare a function that returns a type by value with only a forward declaration. Ex:

struct B;

struct A {
A(const B &) {}
B toB();
};

struct B {
B(const A &) {}
A toA();
};

B A::toB() {
return B(*this);
}

A B::toA() {
return A(*this);
}

Share this post


Link to post
Share on other sites
*confusion*

Boy do I ever feel dumb. That was the first thing I tried, but the compiler kept pointing to the method declaration and saying "invalid use of an incomplete type!" I must have been doing something else wrong though.

Thanks for your quick reply, I appreciate it!

Share this post


Link to post
Share on other sites
Quote:
Original post by Puck
A& operator+(const A& rhs) {
return A(*this) += rhs;
}

Returning a temporary by reference? This simply can't be right.

Share this post


Link to post
Share on other sites
Quote:
Original post by Puck
Boy do I ever feel dumb. That was the first thing I tried, but the compiler kept pointing to the method declaration and saying "invalid use of an incomplete type!" I must have been doing something else wrong though.


Trying to define the function inside the struct maybe? :)

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!