Results 1 to 2 of 2

Thread: Problem with anonymous union in C++

  1. #1
    Join Date
    May 2008
    Posts
    25

    Problem with anonymous union in C++

    Hi

    I wanted to use an anonymous union within an structure something like
    below -

    struct Test
    {
    union
    {
    std::string user; //char user[50];
    std::string role; //char role[50];
    };
    std::string desc;
    };

    Whenever I use, built in data types such as int, char etc, it works perfectly fine, but for user defined dataypes, it gives me error -

    error: member `std::string Test::<anonymous union>::user' with
    constructor not allowed in union
    error: member `std::string Test::<anonymous union>::user' with
    destructor not allowed in union
    error: member `std::string Test::<anonymous union>::user' with copy
    assignment operator not allowed in union
    error: member `std::string Test::<anonymous union>::role' with
    constructor not allowed in union
    error: member `std::string Test::<anonymous union>::role' with
    destructor not allowed in union
    error: member `std::string Test::<anonymous union>::role' with copy
    assignment operator not allowed in union

    Can someone give any idea, how to sort out the problem?

  2. #2
    Join Date
    Oct 2008
    Posts
    77

    Re: Problem with anonymous union in C++

    There's no workaround for this. Why don't you use the following:

    class Test
    Code:
    {
    private:
    std::string user_or_role;
    public:
    std::string& get_user ()
    {
    return user_or_role;
    }
    
    std::string& get_role ()
    {
    return user_or_role;
    }
    std::string desc;
    };

Similar Threads

  1. How to define Union?
    By seema_thk in forum Software Development
    Replies: 5
    Last Post: 15-12-2009, 02:07 PM
  2. Confused about difference between structure and union
    By rashmi_ay in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 01:37 PM
  3. Using Internal and External union SQL Queries
    By Elbanco in forum Software Development
    Replies: 4
    Last Post: 06-11-2009, 10:45 PM
  4. Differences between UNION and JOINS?
    By Swati in forum Software Development
    Replies: 3
    Last Post: 13-02-2009, 05:58 PM
  5. Should IT form a union?
    By absolute55 in forum Education Career and Job Discussions
    Replies: 1
    Last Post: 05-09-2008, 07:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,035,278.46467 seconds with 17 queries