Jump to content
  • Advertisement
Sign in to follow this  
stupid_programmer

Using variables in class before declared

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

One of the functions in my class needs to be passed a struct that is delcared private in the same class. But unless the struct is declared first I get an error, I thought there was a way to get around this. This is the class that gives me an error:
class Test
{
public:
     void Func1(my_struct data);

private:
     struct my_struct {
          int x;
          int y;
     };
};
I can fix it by doing this, but I don't want to believe its the best way:
class Test
{
private:
     struct my_struct;

public:
     void Func1(my_struct data);

private:
     struct my_struct {
          int x;
          int y;
     };
};

Share this post


Link to post
Share on other sites
Advertisement
Hold on and think about this carefully. If the struct declaration is private, how is any outside code going to be able to call the function? If it won't, why is the function public?

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Hold on and think about this carefully. If the struct declaration is private, how is any outside code going to be able to call the function? If it won't, why is the function public?


agreed, if the function is supposed to do anything useful to outside code, it should be written as follows


struct my_struct {
int x;
int y;
};

class Test
{
public:
void Func1(my_struct data);
};

Share this post


Link to post
Share on other sites
Not necessarily, it can still be a member of the class if that makes sense. But it should have the same visibility as the functions it is used with.

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!