在基类和派生类之间切换 [英] switching between base and deriven classes

查看:98
本文介绍了在基类和派生类之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在从C#转到C ++,但在调整时遇到了一些麻烦.


我知道下面的代码会遇到很多错误,但这只是一般的想法
在我想做的事情后面.

Hello,

I am moving from c# to c++ and I am having a little trouble adjusting.


I know the code below would through a whole bunch of errors, buts its the general idea
behind what I am trying to do.

class base
{

};

class deriven1 : public base
{

 public:
   int getint() {return num}

 private:
   int num;

};


class deriven2 : public base
{
public:
   string getstr() {return str}

 private:
   string str;
};


int main()
{
  base b = get_deriven_1_or_2();



  if(b is deriven1)
   {
     deriven1 d1 = (deriven1)b;
      
     int i = d1.getint();
     //do stuff

   }
  if(b is deriven2)
   { 
     deriven2 d2 = (deriven2)b;
     string s = d2.getstr();
     //do stuff


    }

}




所以基本上我想在不丢失任何数据的情况下,从派生变回基本,再回到派生.

我有一个执行类似操作的ac#程序,但不知道如何在c ++中执行该操作,因此将不胜感激!




so basically I am trying to change from a deriven to a base back to a deriven with out losing any data.

I had a c# program that did something like it but I have no clue how to do it in c++, so any help would be greatly appreciated!

推荐答案

在谈论非托管C ++时,您可以执行以下操作:

If we''re talking about unmanaged C++, you can do this:

class deriven3 : public deriven1, deriven2
{
}



然后您可以同时访问string属性和int属性,如下所示:



and then you can get to both the string property and the int property, like so:

int x = d3.getint();
string y = d3.getstr();



如果我们谈论的是托管C ++,则必须创建接口,因为不支持多类继承.但是,您可以从一个类和一个或多个接口继承.



If we''re talking about managed C++, you have to create interfaces because multiple class inheritence is not supported. However, you can inherit from one class and one or more interfaces.


此代码甚至无法编译.例如,缺少运算符后的;"终止符.如果您需要更好的答案,请使用真正的编译器检查代码.

现在,您的工作1)与C#,C ++(您将在此处尝试使用)或C ++/CLI之间的差异无关; 2)您的代码根本不是面向对象的;考虑到根本没有类,代码的质量将完全相同.

当使用面向对象的方法时,很少使用类型大小写和运算符"is"或"as".在C ++中,没有这样的运算符,但是可以使用dynamic_cast<>的形式进行动态类型转换,请参见 http://en. wikipedia.org/wiki/Dynamic_cast [ ^ ].

就是说,即使在C#中,您也不知道如何使用OOP.使用派生类的想法是提供一个基类,该基类表示层次结构的通用接口,并使用虚拟方法和重写来提供 late绑定,请参见 ^ ].您应该知道编译时运行时类型之间的区别.这个想法是使用某种基本类型作为变量的编译时类型,但是实例化派生类型之一.

在C ++(不是C ++/CLI)中用作.NET接口的构造由不具有数据且仅具有纯虚拟函数的类表示,请参见http://en.wikipedia.org/wiki/Pure_virtual_method#C.2B.2B [
还有很多.我建议您使用C#,C ++/CLI,.NET和C ++的OOP的好手册(对于入门者来说仅选其中一本),并了解OOP的实际工作原理.您将真正需要它.

您还需要进行一些更改,以在您的特定情况下获得实用的建议,但是在这种情况下,您需要解释最终目标.在后续问题中,请不要使用太多的类概念(您还真的还不知道它到底是什么),而是更多地关注应用程序方面.

祝你好运,
—SA
This code will not even compile. For example '';'' terminators after operators are missing. If you need better answers, please check up your code using a real compiler.

Now, what you do 1) is irrelevant to the differences between C#, C++ (which you''re trying to use here) or C++/CLI; 2) you code is not object-oriented at all; consider you do not have classes at all, the quality of your code will be exactly the same.

When you use object-oriented approach you use type case and operator "is" or "as" very rarely. In C++, there is no such operators, but the dynamic typecast is available in the form of dynamic_cast<>, see http://en.wikipedia.org/wiki/Dynamic_cast[^].

Now, that said, even in C# you did not know how to use OOP. The idea of using derived class is providing a base class which represent the common interface for the hierarchy and using virtual methods and overriding to provide late binding, see http://en.wikipedia.org/wiki/Late_binding[^]. You should know the difference between compile-time and run-time types. The idea is using some base type as a compile-time type for a variable, but instantiate one of derived types.

The construct working as a .NET interface in C++ (not C++/CLI) is represented by the class having no data and only pure-virtual functions, see http://en.wikipedia.org/wiki/Pure_virtual_method#C.2B.2B[^] and the section "Abstract classes and pure virtual functions".

There is a lot more to it. I suggest you take a good manuals for OOP in C#, C++/CLI, .NET and C++ (pick just one for starters) and learn how OOP really works. You will really need it.

You also have some change to get a practical advice in your particular case, but in this case you will need to explain your ultimate goal. In your follow-up question, please do not use the much of the notion of the class (you don''t really know what is it — just yet) but focus more on the application aspects.

Good luck,
—SA


通常来说,在标准的非托管C ++中,无法将它们区分开,因为它们只是指针.那是使用面向对象编程的优点之一,即多态性,但这取决于设计者来跟踪其派生类可以成为的对象类型.

John的解决方案很好,但是还有其他跟踪方法,只需要有一些创造力即可.
Generally speaking, in standard, unmanaged C++ there''s no way to tell them apart, since they''re just pointers. That''s one of the advantages of using object oriented programming, polymorphism, but its up to the designer to keep track of the type of objects that his derived classes can become.

John''s solution is nice, but there''s other ways to keep track, just have to be a bit inventive.
class base
{
 protected:
 int m_type;
 
 public:
 base(){m_type=0;}
};
class derived1 : public base
{
 public:
 derived1(){m_type=1;}
}
class derived2 : public base
{
 public:
 derived2(){m_type=2};
}



您甚至可以在基础中添加枚举受支持的类型的列表.



You can even add an enumerated list of supported types in the base.

class base
{
 enum TYPE{BASE =0, DER1, DER2, DER_MAX=DER2};

 protected:
 TYPE m_type;

 public:
 base(){m_type=BASE;} //And just define the others accordingly
};



...而且我同意SA,使用派生类的全部要点是它们共享相同的通用方法.



...and I agree with SA, the whole point of using derived classes is that they share the same common methods.


这篇关于在基类和派生类之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆