可重用的构造函数 C++

发布于 2024-12-08 01:45:56 字数 478 浏览 1 评论 0原文

OOP 的基石之一是重用代码,而不是一遍又一遍地重复代码。因此,您的项目会缩短并变得更具可读性。

C++ 为您提供了重用方法而不是重复代码所需的所有工具。尽管说到构造函数我不知道如何重用它们。

不是谈论遗产或如何向父亲传达信息。我说的是重用类本身的构造函数。

JAVA 中的类比是这样的:

public Foo() {
    this(0,0,0);//Not needed in this case, just to clarify
}

public Foo(Foo f){
    this(f.getA(), f.getB(), f.getC());
}

public Foo(int a, int b, int c) {
    this.a = a;
    this.b = b;
    this.c = c;
}

我的问题是,C++ 中是否有任何语法允许您这样做?

One of the corner stones of OOP is reusing code instead of repeat it over and over. Thus, your projects shorten and get more readable.

C++ gives you all the tools you need to reuse methods instead of repeating the code. Although when it comes to constructors I do not know how to reuse them.

I am not talking of heritage or how to send a message to the father. I am talking about reusing the constructor of the class itself.

The analogy in JAVA is something like this:

public Foo() {
    this(0,0,0);//Not needed in this case, just to clarify
}

public Foo(Foo f){
    this(f.getA(), f.getB(), f.getC());
}

public Foo(int a, int b, int c) {
    this.a = a;
    this.b = b;
    this.c = c;
}

My question is, is there any syntaxis in C++ that allows you to do so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

满栀 2024-12-15 01:45:56

C++11 添加了构造函数委托和构造函数继承

要继承构造函数,需要使用声明:

class Base { ... };

class Derived : public Base
{
    using Base::Base;
};

要委托,请使用构造函数初始值设定项,但在同一个类中指定另一个构造函数,而不是任何子对象(所有基对象)并且成员子对象将由委托给的构造函数进行初始化):

class Another : public Base
{
    int member;
    Another(int x)
        : Base(), member(x) // non-delegating constructor initializes sub-objects
    {}


    Another(void)
        : Another(5) // delegates -- other constructor takes care of Base and member
    {}
};

并且完美的转发也可以派上用场。

C++11 has added constructor delegation and constructor inheritance.

To inherit constructors, a using-declaration is required:

class Base { ... };

class Derived : public Base
{
    using Base::Base;
};

To delegate, use the ctor-initializer, but specify another constructor in the same class, instead of any subobjects (all base and member subobjects will be initialized by the constructor delegated to):

class Another : public Base
{
    int member;
    Another(int x)
        : Base(), member(x) // non-delegating constructor initializes sub-objects
    {}


    Another(void)
        : Another(5) // delegates -- other constructor takes care of Base and member
    {}
};

And perfect forwarding can also come in handy.

月亮坠入山谷 2024-12-15 01:45:56

其他人已经回答了有关 C++11 的问题,但对于 C++03,有一个可能的解决方法:使用具有所需构造函数的基类。

struct foo_base {
    foo_base(int a, int b, int c) : a(a), b(b), c(c) { }
    int a, b, c;
};

struct foo : foo_base {
    foo() : foo_base(0, 0, 0) { }
    foo(const foo& other) : foo_base(other.a, other.b, other.c) { }
    foo(int a, int b, int c) : foo_base(a, b, c) { }
};

当然,您需要考虑它是否值得为您的目的使用样板。

Others already answered about C++11, but for C++03 there's a possible workaround: using a base class with needed constructor(s).

struct foo_base {
    foo_base(int a, int b, int c) : a(a), b(b), c(c) { }
    int a, b, c;
};

struct foo : foo_base {
    foo() : foo_base(0, 0, 0) { }
    foo(const foo& other) : foo_base(other.a, other.b, other.c) { }
    foo(int a, int b, int c) : foo_base(a, b, c) { }
};

Of course, you need to consider whether it's worth the boilerplate for your purposes.

想挽留 2024-12-15 01:45:56

当前编译器普遍接受的灵魂是这样做:

class Bar{
pubilc:    
Foo() {
   init(0,0,0);
}

Foo(const Foo &f){
  init(f.getA(), f.getB(), f.getC());
}

Foo(int a, int b, int c) {
  init(a,b,c);
}

private:

void init(int a, int b, int c){
  this->a = a;
  this->b = b;
  this->c = c;
}
};

虽然在这个例子中这看起来像是过度杀戮,但这只是因为例子的简单性。在现实世界的应用程序中,这实际上会带来减少重复代码的好处。

The generally accepted soultion for current compilers is to do this:

class Bar{
pubilc:    
Foo() {
   init(0,0,0);
}

Foo(const Foo &f){
  init(f.getA(), f.getB(), f.getC());
}

Foo(int a, int b, int c) {
  init(a,b,c);
}

private:

void init(int a, int b, int c){
  this->a = a;
  this->b = b;
  this->c = c;
}
};

While this may seem like over kill in this example, that is only because of the simplicity of the example. In a real world application this would actually bring benefits in terms of reduction of repeated code.

ゝ杯具 2024-12-15 01:45:56

OK C++11 可以满足您的需求。

但你的简单案例有一个简单的解决方案:

/* This one is covered by providing default parameters see below.
public Foo() {
    this(0,0,0);//Not needed in this case, just to clarify
}

 This is done automatically by the compiler.
 You do not need to write any code for this:
public Foo(Foo f){
    this(f.getA(), f.getB(), f.getC());
}
The compiler generated version actually looks like this:
public Foo(Foo const& f)
    : a(f.a)
    , b(f.b)
    , c(f.c)
{}



*/

// Now you can use all three methods and they work fine:
public Foo(int a = 0, int b = 0, int c = 0)
    : a(a)
    , b(b)
    , c(c)
{}

F   f1;        // default construct no parameters: uses the three parameter version
F   f2(f1);    // Copy constructed. Generated by the compiler.
F   f3(1,2,3); // Nomal constructor

OK C++11 covers what you need.

But your simple case has an easy solution:

/* This one is covered by providing default parameters see below.
public Foo() {
    this(0,0,0);//Not needed in this case, just to clarify
}

 This is done automatically by the compiler.
 You do not need to write any code for this:
public Foo(Foo f){
    this(f.getA(), f.getB(), f.getC());
}
The compiler generated version actually looks like this:
public Foo(Foo const& f)
    : a(f.a)
    , b(f.b)
    , c(f.c)
{}



*/

// Now you can use all three methods and they work fine:
public Foo(int a = 0, int b = 0, int c = 0)
    : a(a)
    , b(b)
    , c(c)
{}

F   f1;        // default construct no parameters: uses the three parameter version
F   f2(f1);    // Copy constructed. Generated by the compiler.
F   f3(1,2,3); // Nomal constructor
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文