Solved

problem with unions

Posted on 1998-05-19
5
905 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

708 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now