VC ++ .NET中的等余变数 [英] COlevariant equivalent in VC++ .NET

查看:93
本文介绍了VC ++ .NET中的等余变数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是.NET的新手,正在使用数据库应用程序.我想知道是否有任何与Visual C ++中的MFC的Colevariant等效的数据类型?

非常感谢任何帮助.创建表示变量类型的简单类层次结构并不难:

class CVariantGeoObject {
public:
   enum EGeoType {
      EGT_POINT,
      EGT_VECTOR
   };
   virtual EGeoType getType() const = 0;
   virtual void doWork() = 0;
};
class CPoint : public CVariantGeoType {
   virtual EGeoType getType() const { return EGT_POINT; }
   virtual void doWork(); // implemented elsewhere
};
class CVector : public CVariantGeoType {
   virtual EGeoType getType() const { return EGT_VECTOR; }
   virtual void doWork(); // implemented elsewhere
};
// ...
int main() {
   // read some variant type from database - could be a point or a vector
   CVariantGeoType* myData = 0;
   if (field type is point) {
      myData = new CPoint;
      // read point data to intialize this point object
   }
   else if (field type is vector) {
      myData = new CVector;
      // read vector data to intialize this vector object
   }
   // now do some work
   if (myData != 0) {
      myData->doWork();
      // and clean up:
      delete myData;
   }
}


永远不要使用变量!那是另一个愚蠢的发明(第一个愚蠢的是以空字符结尾的字符串,但是您必须将它们用于与遗留项进行互操作).

—SA


Hi,

I am new to .NET and am using a database application. I wanted to know if there was any datatype which is an equivalent of Colevariant of MFC in Visual C++?

Any help is greatly appreciated.

解决方案

No solution, just a comment: Variant types have always be a horrible idea in any language, and this is especially true in C++! It''s not that hard to create a simple class hierarchy representing a variant type:

class CVariantGeoObject {
public:
   enum EGeoType {
      EGT_POINT,
      EGT_VECTOR
   };
   virtual EGeoType getType() const = 0;
   virtual void doWork() = 0;
};
class CPoint : public CVariantGeoType {
   virtual EGeoType getType() const { return EGT_POINT; }
   virtual void doWork(); // implemented elsewhere
};
class CVector : public CVariantGeoType {
   virtual EGeoType getType() const { return EGT_VECTOR; }
   virtual void doWork(); // implemented elsewhere
};
// ...
int main() {
   // read some variant type from database - could be a point or a vector
   CVariantGeoType* myData = 0;
   if (field type is point) {
      myData = new CPoint;
      // read point data to intialize this point object
   }
   else if (field type is vector) {
      myData = new CVector;
      // read vector data to intialize this vector object
   }
   // now do some work
   if (myData != 0) {
      myData->doWork();
      // and clean up:
      delete myData;
   }
}


Do not use variants, ever! That is another stupid invention (first stupid was null-terminated strings, but you''re bound to use them for inter-operation with legacy).

—SA


这篇关于VC ++ .NET中的等余变数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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