将基本类的对象扩展到复杂类的对象

发布于 2024-12-10 20:07:38 字数 506 浏览 0 评论 0原文

我怎样才能做到这一点?我想到了复杂类中的一种方法,它将基本对象的每个变量复制到复杂对象,但这似乎有点不方便。

class Basic
{
  //basic stuff
}

class Complex : public Basic
{
  //more stuff
}

Basic * basicObject = new Basic();
//now "extending" basicObject and "cast" it to Complex type
//which means copy everything in basicObject to an complexObject

或者类似的东西:

Complex * complexObject = new Complex();
complexObject.getEverythingFrom(basicObject);

似乎太不方便了,因为每次我更改Basic类时,我也必须更改这个“复制”方法。

how can I do that? I thought of an method in the complex class which copies every variable of the basic object to the complex object, but that seems a little bit to inconvenient.

class Basic
{
  //basic stuff
}

class Complex : public Basic
{
  //more stuff
}

Basic * basicObject = new Basic();
//now "extending" basicObject and "cast" it to Complex type
//which means copy everything in basicObject to an complexObject

or something like:

Complex * complexObject = new Complex();
complexObject.getEverythingFrom(basicObject);

seems to be too inconvenient, because everytime I change the Basic class, I have to change this "copy" method too.

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

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

发布评论

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

评论(2

蹲在坟头点根烟 2024-12-17 20:07:38

定义要在受保护部分中的类之间共享的值,如下所示:

class Base
{
public:
int myPublicShared1;
int myPublicShared2;

Base& operator = (Base& other)
{
    // Copy contents in base across
    return *this;
}
protected:
int myShared1;
int myShared2;

private:
int notShared1;
int notShared2;
};

class Derived : public Base
{
public:
Derived& operator = (Derived& other)
{
    Base::operator = (other);
    // copy the rest of variables specific to Derived class.
}
Derived& operator = (Base& other)
{
    Base::operator = (other);
}
// Derived now has all variables declared in Base's public and protected section
};

Define the values you want to share between the classes in protected section like so:

class Base
{
public:
int myPublicShared1;
int myPublicShared2;

Base& operator = (Base& other)
{
    // Copy contents in base across
    return *this;
}
protected:
int myShared1;
int myShared2;

private:
int notShared1;
int notShared2;
};

class Derived : public Base
{
public:
Derived& operator = (Derived& other)
{
    Base::operator = (other);
    // copy the rest of variables specific to Derived class.
}
Derived& operator = (Base& other)
{
    Base::operator = (other);
}
// Derived now has all variables declared in Base's public and protected section
};
无妨# 2024-12-17 20:07:38

在 C++ 中,对象不能更改其类型。

因此,要么重写程序以立即创建 Complex 对象,要么创建一个复制构造函数,以便可以执行以下操作:

new Complex(*basicObject);

旁注:从像extend这样的词和new的用法来看,你似乎来自java世界。不要错误地认为在 Java 中做事的方式也是在 C++ 中做事的方式。

In C++, objects can not change their type.

So either you rewrite your program to right away create Complex objects, or you create a copy ctor so that you can do:

new Complex(*basicObject);

side note: from words like extend and the usage of new it seems you come from a java world. Don't make the mistake of thinking that how you do things in java is also how you do it in C++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文