Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

Compilation error and refernece to a copiedct'r variable?

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

I have the following code:
		class B {
		public:
			DWORD Num;
		};
		class C {
			public:
			DWORD counter;
			B ReturnClass () { B b; this->counter = 0; b.Num=2; return b;};
			void UseClass (B & b) { this->counter = b.Num;};
		};

		C c;
		c.UseClass (c.ReturnClass());
I get the following errors: error C2065: 'B' : undeclared identifier E:\Projects\Engine2\V0\Main.cpp(208) : error C2146: syntax error : missing ';' before identifier 'b' E:\Projects\Engine2\V0\Main.cpp(208) : error C2065: 'b' : undeclared identifier E:\Projects\Engine2\V0\Main.cpp(208) : error C2228: left of '.Num' must have class/struct/union type Why do I get these errors? Would the call to UseClass(c.ReturnClass()) compile if I fixed the class error? Is it not good to put in a reference paramter a return by value of a function? (or copyct'r return value). I would appreaciate help. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
I can't say why that compile-time error would occur, but I do know that you'll get runtime errors with it. In ReturnClass(), the object b is created on the stack for the duration of that function alone, and destructed when you leave the function, so by the time you get back to the call to c.UseClass() the object is no longer valid.

Share this post


Link to post
Share on other sites
Quote:
Original post by TDragon
I can't say why that compile-time error would occur, but I do know that you'll get runtime errors with it. In ReturnClass(), the object b is created on the stack for the duration of that function alone, and destructed when you leave the function, so by the time you get back to the call to c.UseClass() the object is no longer valid.

When you return a class by value, you use the copy constructor which suppose to create a new class, it is different from the class which you return in the method and that was declared inside the method.

Share this post


Link to post
Share on other sites
Oops, silly me...I've been thinking too hard today. That would only happen if you returned the address of the object. Sorry 'bout that.

Share this post


Link to post
Share on other sites
(My terminology might not be exact here as I'm not familiar enough with the relevant portions of the standard).

A fuction call which returns a class by value returns an rvalue. An rvalue may be bound to a const reference*, in which case either the existing rvalue is directly bound or a new temporary is copy-constructed from the rvalue and bound to the const reference. In either case when the const reference is a function parameter the object bound to the const reference persists until the completion of the full expression containing the call to the function for which the const reference is a parameter.

In English, your code would be entirely valid if UseClass took its parameter by const reference.

There is nothing in the code you posted that would cause the errors you're getting. The error must be elsewhere.

Enigma

*At least I think it has to be a const reference. Of my three compilers one issues an error, one a warning and one compiles it with no errors or warnings. If it doesn't have to be a const reference then the code is valid as-is.

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!