Thread: Copy constructors and private constructors

  1. #1
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Copy constructors and private constructors

    I'm creating a few wrapper classes for the SDL library in anticipation of making a game or something, right now i'm wrapping the video init code. class display has private constructors, public named constructors return a pointer to a display class. Only one display class can be initilized at a time.

    Question is this; do I really need a copy constructor/could the copy constructor ever be called, if all the user ever gets is a pointer to a display object?

    If a copy constructor could be called, how can I handle it? Only one object can be initilized at a time... copy the current object and kill it? (delete object;) throw an exception?

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    You say a SDL wrapper...

    My SDL wrapper ZEngine has tons of work into it, email me to talk about working on it or just using it. Depending on how you set it up you may never need a copy constructor.. I'd recommend a singleton approach.

    Singleton is a class that there can only be one instance of, and works very well in the design of the core part of an engine.

    [email protected], we can talk SDL. (ZEngine has tons of stuff, SDL addons, INI reading, loading data from zip files, soon a GUI)

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, you might define a do-nothing copy constructor to inhibit any default behaviour.

    [quote]
    If a copy constructor could be called, how can I handle it? Only one object can be initilized at a time... copy the current object and kill it? (delete object throw an exception?
    [quote]

    Why would you want to? Anyway, nothing stopping this:

    Display * one = Get(); //..."the pointer"
    char * chunk = new char[sizeof(Display)];
    Diplay * two = (Display*)chunk;
    memcpy(two, one, sizeof(Display));
    two->blit();
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Yes, a singleton is exactly what I need. Thanks for the terminology, it yeilded many search results. Most examples use either an unnamed class or a static reference count, I was using the reference count prior to all of this.

    I'd like to take a look at your library, I'll probably learn something from it. I'll give you an email later on. Do you have either ICQ or MSN Messenger?

    I searched for SDL wrapper libraries before starting, one interesting one came up; SEL (http://sel.sourceforge.net I think) but it looked as if it was written by beginners... for instance the display class didn't have any saftey mechanisms on it like i'm trying to implement.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Singletons are real handy for situations where only 1 object can be in usage.....you can impose a reference counting control, but if the count can only be 1, then here's a simpler method;

    Code:
    #include <iostream>
    
    class foobar{
    	foobar(){}//Private...so only foobar::GetFoobar can use
    	foobar(const foobar&);//as above
    public:
    	void Sing(){std::cout << "lalalalalala" << std::endl;}
    	
    	static foobar& GetFoobar();//static member function to access singleton
    };
    
    foobar& foobar::GetFoobar(){
    	static foobar f;//this is created once on the first call to func
    	return f;//return reference
    }
    
    int main(){
    
    	//foobar a; \\cant create instance
    	
    	foobar& b = foobar::GetFoobar();//can get and use reference	
    	b.Sing();
    	
    	foobar::GetFoobar().Sing();//Can get and use with single call
    	
    }
    As you see, the constructors are all private.....so the only functions with access to create a foobar object are the static member functions of foobar itself.....so if you define a static member func that creates a static object, it will be created only once when the function is first called....you can then use that function and all it will do is return the reference to the single object.......quite nice and clean...

  6. #6
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Thanks for the help guys, I chose a simple reference count for this. It seems to be working perfectly. I created a class, singleton and subclassed display with it; as I can see needing more singletons in the future.
    Last edited by Eibro; 11-24-2002 at 03:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. still confused with constructors and private data members
    By freddyvorhees in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2008, 09:29 AM
  2. Hiding constructors, exposing copy operations
    By Mario F. in forum C++ Programming
    Replies: 10
    Last Post: 07-23-2007, 07:44 AM
  3. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 05:34 PM
  4. Copy constructors; Best practices (and private)
    By Mario F. in forum C++ Programming
    Replies: 15
    Last Post: 06-23-2006, 04:42 PM
  5. 'Passing by Refrence for Efficiency', Copy Constructors?
    By Zeusbwr in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2004, 07:11 AM