Forward class declarations

B

Belebele

Why would the compiler give an error in the code below? Why would the
class declaration be required to compile the code below:

class Foo;
Foo& g();
void f()
{
g(); // use of undefined type 'Foo'
}

I am using VC++ 6.0 and VC++ 8.0

Thanks
 
P

peter koch

Why would the compiler give an error in the code below? Why would the
class declaration be required to compile the code below:

class Foo;
Foo& g();
void f()
{
        g(); // use of undefined type 'Foo'

}

I am using VC++ 6.0 and VC++ 8.0

Thanks

Because the compiler does not know how to destroy the result of g() -
an object of type Foo.

/Peter
 
B

Belebele

Because the compiler does not know how to destroy the result of g() -
an object of type Foo.

g returns a reference to an object of type Foo. The compiler should
not add code to call the destructor of the returned object.
 
P

peter koch

g returns a reference to an object of type Foo. The compiler should
not add code to call the destructor of the returned object.

True. I overlooked the ampersand. Sorry I can't help you - this looks
like a bug to me.

/Peter
 
B

Belebele

in my compiler, it complains that `undefined g()', a link-time error.
btw, my compiler is mingw-g++.

That tells me that the problem I had may be caused by a VC++ bug, or
rather, incomplete compliance to the standard.
if you have defined g(), then it should depend on how you return the
result of g().

It should not depend on anything. The returned value of g is a
reference and, as far as I know, reference and pointers should be
passed around regardless of the definition of the class of the
underlying object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top