简体   繁体   English

C++ 中的“扩展”结构

[英]“Extend” Struct in C++

I say "extend" because after a google search I'm not sure that is the proper name for what I'm trying to accomplish.我说“扩展”是因为在谷歌搜索之后我不确定这是我想要完成的事情的正确名称。

Basically what I'm trying to do is create a struct (just for fun, let's also include classes in this question) with some blank variables in it, then make another struct or class that carries on where the parent left off, filling in those variables.基本上我想要做的是创建一个结构体(只是为了好玩,让我们也包括这个问题中的类),其中包含一些空白变量,然后创建另一个结构体或类,在父级停止的地方继续,填充那些变量。

So something like this:所以像这样:

struct parent{

    int foo;
    int bar;
};

struct daughter : parent{
    foo=1;
    bar=3;
};

struct son : parent {
    foo=10;
    bar=20;

    int foobar;
};

and later on, I might need to branch out this tree further:稍后,我可能需要进一步扩展这棵树:

struct grandson : son {
    foo=50;
    bar=900;
    foobar=10000;
};

What would be the correct way to go about this?解决这个问题的正确方法是什么?

EDIT:编辑:

@Everyone: So, I can explain what I'm trying to do, and you can counter it with better ways, and who knows, maybe those ways are much better. @Everyone:所以,我可以解释我想要做什么,你可以用更好的方法来反击它,谁知道呢,也许这些方法要好得多。 However, first, right off the bat, I'm just curious if what I'm asking is possible...然而,首先,马上,我只是好奇我问的是不是可能......

And something I left out:我遗漏了一些东西:

struct parent{
    int foo;
    int bar;
    int foobar;

    int add(int x, int y){
        return x+y;
    };

struct son : parent {
    foo=12;
    bar=13;
    foobar=add(foo,bar);
}

I think you may want to use constructors to initialize values like that.我认为您可能想使用构造函数来初始化这样的值。 Something like this:像这样的东西:

struct daughter : public parent {
   daughter() {
      foo=1;
      bar=3;
   }
};

(btw I know the 'public' keyword isn't necessary, I included it only because I like to keep my intent explicit; otherwise I always wonder later if I just forgot to specify it) (顺便说一句,我知道“public”关键字不是必需的,我包含它只是因为我喜欢保持明确的意图;否则我以后总是想知道我是否只是忘记指定它)

Your parent and daughter are essentially the same type besides their initial class values.除了它们的初始类值之外,您的parentdaughter本质上是相同的类型。 The same goes for grandson and son . grandsonson

Why not use a constructor for that task?为什么不为该任务使用构造函数?

struct parent
{
    parent () : foo(), bar() {}
    parent (int the_foo, int the_bar) 
      : foo(the_foo), bar(the_bar) {}
    int foo;
    int bar;
};

struct son : public parent
{
    son () : parent(10,20), foobar(foo+bar) {}
    son (int the_foobar) 
      : parent(10,20), foobar(the_foobar) {}
    int foobar;
}

int main ()
{
  parent p;
  parent daughter(1,3);
  son s;
  son grandson(10000);
}

  • Don't use a struct for inheritance: use a class instead.不要使用struct进行继承:改用class This undermines the protection level in the inheritance hierarchy because the default privacy level of members of a struct is public .这破坏了继承层次结构中的保护级别,因为结构成员的默认隐私级别是public C programmers reading C++ code will assume the structs aren't inherited, have methods, ETC.阅读 C++ 代码的 C 程序员将假定结构不是继承的,有方法等。
  • Make all members private (if not modified by an inherited class) or protected (if modified by an inherited class) to prevent accidental modifications by the instantiators of the class.将所有成员设为私有(如果未被继承类修改)或受保护(如果被继承类修改)以防止类的实例化器意外修改。
  • Make the constructors public in order for the classes to be instantiated, if the classes are supposed to be instantiable by any other code.如果类应该可以由任何其他代码实例化,则将构造函数设为public以便实例化类。

class parent{

protected:
    int foo;
    int bar;
public:
    parent() {
        foo = 0;
        bar = 0;
    }
};

class daughter : parent{
private:
public:
    daughter() {
        foo=1;
        bar=3; 
    }
};

class son : parent {
private:
    int foobar;
public:
    son () {
        foo=10;
        bar=20;
        foobar = 0;
    }
};

In C++, structs are treated as classes where all members are public by default.在 C++ 中,结构被视为类,默认情况下所有成员都是公共的。

So you can get the behaviour you are wanting.所以你可以得到你想要的行为。

However, you need to use a constructor to initialize the variables, you can't do it directly like you have written.但是,您需要使用构造函数来初始化变量,不能像您写的那样直接进行。

So this:所以这:

struct parent{
int foo;
int bar;
int foobar;

int add(int x, int y){
    return x+y;
};

struct son : parent {
foo=12;
bar=13;
foobar=add(foo,bar);

Needs to be written as需要写成

struct parent
{
  int foo;
  int bar;
  int foobar;

  int add(int x, int y){
    return x+y;
  };
};

struct son : parent 
{
  son()
  {
    foo = 12;
    bar = 13;
    foobar = add(foo,bar);
    // or just: foobar = foo+bar;
  }
};

But in this case, a son and a parent are actually an instantiation of the same struct.但在这种情况下,儿子和父母实际上是同一个结构的实例化。 Which is rather silly.这是相当愚蠢的。 Are you going to be adding more fields to son because that's what inheritance is about.您是否要为儿子添加更多字段,因为这就是继承的含义。 In your case your father and son are identical but just hold different state.在您的情况下,您的父亲和儿子是相同的,但只是处于不同的状态。 On the other hand if son needs to hold other data then it needs to inherit.另一方面,如果儿子需要持有其他数据,那么它需要继承。

Also, if you're going to start treating structs as classes by adding methods then why not just make them classes.此外,如果您打算通过添加方法开始将结构视为类,那么为什么不直接将它们设为类。

I don't see a necessity for inheritance in this case.在这种情况下,我没有看到继承的必要性。 If you just want to get instances of parent with specific values, then a simple function may be all that you need:如果您只想获取具有特定值的parent实例,那么您可能只需要一个简单的函数:

parent get_daughter() {
    // note that this syntax requires c++11
    // in earlier standards you may want to add a constructor to the class
    return {1, 3};
};

son introduces a new member so inheritance is appropriate. son引入了一个新成员,因此继承是合适的。

I think languages like java and C# may have some wage definitions of local object initialization.我认为像 java 和 C# 这样的语言可能对本地对象初始化有一些工资定义。 But, according to me C++ has got the 'best' way of explain member variable (not static) initialization.但是,据我所知,C++ 已经获得了解释成员变量(非静态)初始化的“最佳”方式。 Just do them inside of constructors.只需在构造函数中执行它们。 Very clear, intuitive and less error-prone.非常清晰、直观且不易出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM