0

I have a small issue with my C++ code.

class Command {
public:
    virtual void start(CommandDesc userinput, Converter* convertobj) = 0;
    virtual void help(int option) = 0;
};


struct CommandDesc
{
    std::string name;
    std::string description;
    Command* comobj;   //Issue is here
};

If I define the structure before the class, I won't be able to define a member

Command* comobj; 

If I define after the class, I wont be able to pass instance of struct to method with

virtual void start(CommandDesc userinput, Converter* convertobj) = 0;

What can you suggest? Is there any way to first declare the structure, than define it separately?

4

2 回答 2

7

好的,如果我在类之前定义结构,我将无法定义成员

Command* comobj;

由于 comobj 是一个指针,您可以转发声明Command来解决这个问题。

你会这样做:

class Command;

struct CommandDesc
{
    std::string name;
    std::string description;
    Command* comobj; 
};

class Command {
public:
    virtual void start(CommandDesc userinput, Converter* convertobj) = 0;
    virtual void help(int option) = 0;
};
于 2013-07-07T09:56:17.570 回答
0

是 - 简单 - 前向声明

就放

class Command;

struct CommandDesc
{

 ....

}

class Command {
 As before

};
于 2013-07-07T09:58:08.703 回答