Solved

problem with unions

Posted on 1998-05-19
5
908 Views
Last Modified: 2012-08-14
I'm working on a small C++ programming project in VC++ 5 and I'm running into a little trouble. Within a class I'm defining a union, and its members are instantiated classes. Example:

class clsA
{ ...
};

class clsB
{ ...
};

class clsC
{  union Foo
   {  clsA a;
      clsB b;
   } MyFoo;
};

At compile time, I recieve these errors:
error C2621: union 'Foo' : member 'a' has copy constructor
error C2621: union 'Foo' : member 'b' has copy constructor

Any ideas on how to resolve this problem?
0
Comment
Question by:mnichols
  • 3
5 Comments
 
LVL 1

Expert Comment

by:Nevering
ID: 1164360
can i see your actual code, i did the following, and no errors

class clsA
{
      int a;
      int b;
};

class clsB
{
      int c;
      int d;
};

class CImproView : public CScrollView
{

.... some stuff

      union foo
      {
            clsA a;
            clsB b;
      } myfoo;

.... some more stuff

};
0
 
LVL 22

Accepted Solution

by:
nietod earned 70 total points
ID: 1164361
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1164362
A type (class) that has either a

constructor
destructor
copy constructor

may not be used within a union.  This because only one item of all the items in the union should be initialized (constructed), destroyed (destructor) or copied (copy constructor).  However the compiler does not know which item to use, so it cannot support types with these functions in unions.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1164363
As to how to resolve the problem.... that depends.  What are you trying to do?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1164364
Store a pointer to an object in your union rather than the object itself.

class clsC {
  union Foo {
    clsA* pa;
    clsB* pb;
  } MyFoo;
};

BTW: You don't need 'MyFoo' - you may not need Foo or MyFoo above .. instead just have

class clsC {
  union {
    clsA* pa;
    clsB* pb;
  };
};

Alterntively, you may really be better with a tree of classes with a common subclass.

eg.
class Foo {
 ...
};

class clsA : public Foo {
  ...
};

class clsB : public Foo {
  ...
};

class clsC
  MyFoo* pFoo;
  ...
};

now your pFoo can point to either a clsA or clsB .. and you can use virtual functions and/or RTTI.

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question