Jump to content
  • Advertisement
Sign in to follow this  
Aardvajk

Forward declarations and return values

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

GCC is letting me do something I didn't think was allowed and I was just wondering if this is non-standard or undefined behaviour or not?

I have a Om::Value class, and an Om::ValueList class. ValueList needs to include OmValue.h as it has, for example, Value operator[](int index) method.

But I'm trying to add a ValueList toList() method to Value, which means I have a circular dependancy. But I noticed that if I forward declare ValueList at the top of OmValue.h, GCC is then allowing me to use ValueList as a return type (not a reference or pointer):

class ValueList;

class Value
{
public:
    // snip

    ValueList toList() const; // thought this would throw a "not defined" error?
};
I thought this was not allowed since the compiler would need to know the size of ValueList? Is this non-standard or undefined or something GCC is allowing through an extension, or is this perfectly valid standard C++?

Share this post


Link to post
Share on other sites
Advertisement

It doesn't need to know the size until toList() is actually called. So the declaration is legal, but using the function without the full class definition will give an undefined error.

Share this post


Link to post
Share on other sites
I thought this was not allowed since the compiler would need to know the size of ValueList?

Where does it need that size here?

"ValueList toList() const" says "There is a 'toList' method in the class, it outputs an object of type ValueList". The class definition only needs the size of the (reference to) the 'toList' method. That reference is not changing size depending on the size of the result of the call. The class definition also does not store the result due to this line.

 

In context of an actual call to Value::toList(), the code needs to know the size to create space for the return value. It also needs to know the size in context of the 'toList' method implementation, ie

class Value
{
    ...
    ValueList toList() const
    {
        ...
        return values;
    }
};

but you don't do that either here.

Edited by Alberth

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!