DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2005
    Posts
    33

    Arrow Creating an assignment operator and copy constructor

    Say class B is a singleton
    class B
    { private:
    static B* pInstance;
    public:
    static B* Instance();
    .
    .
    };
    Also say class A holds a reference to B.
    class A
    { private:
    B& refr;
    .
    .
    };
    How do I construct the assignment operator for class A.
    Also how would the copy constructor be coded, for example, as shown below:
    A::A(const A& other) : refr(other.refr)
    { .
    .
    }

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Singleton classes aren't meant to be contained as members of other classes. The whole idea tis to provide a global access function that returns the singleton object.
    However, copying B objects is a trivial operation. Simply use the default assignment operator and copy constructor of B (assuming they're not private, which they ought to be!). The class doesn't contain any nonstatic data members anyway, so copying it merely copies a single random byte that is never accessed or used.
    The real question is why you want to do this. To use A you never even need an instance thereof: all you do is call the Instance() static function like this:
    p=A::Instance();
    So obviously, you can't define a reference to A when there are no instances of A!
    Danny Kalev

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
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links