thing

This is a discussion on thing within the C++ Programming forums, part of the General Programming Boards category; How much so? I think I could come up with some ways of making it less efficient? so it's a ...

  1. #16
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    How much so? I think I could come up with some ways of making it less efficient? so it's a bad idea then? So the way to is to make the getter and calling it in the constructor? Cool, thanks!

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Because it doesn't make sense to call the copy constructor inside a setter function. You are erasing and re-initializing the values of any other data members in the class. That's not what setter functions typically do. If that really is what you want, normally you would make an operator= that takes the new value, or give the function a different name like ReInitialize or something that makes it clear what is happening.

    If you don't mean to re-initialize all the other data members, and you just want to avoid duplicating the code, then follow the advice of others in the thread and just call the setter function from your constructor instead of the other way around. Personally, I would use the initializer list to initialize the value, then have the setter set the value directly, and not worry about code duplication in this particular case (since the code really isn't duplicated).

  3. #18
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    this might work well in certain situations - a delegated construction idiom:

    Code:
    template <class Type>
    inline
    Type &
    construct(Type & obj)
    {
    	return *(new((Type*)&obj)Type());
    }
    
    struct constructed
    {
    	template <class Type>
    	inline
    	constructed(Type & obj)
    	{
    		construct(obj);
    	}
    
    	/* dummy */
    	inline
    	constructed(void)
    	{
    	
    	}
    };
    sample usage:

    Code:
    class thingy : public constructed
    {
    	public:
    
    	int i, & r, * p;
    
    	/* invoked from constructed(*this) */
    	thingy(void)
    	: i(1024), p(&i), r(i)
    	{
    
    	}
    
    	thingy(int j)
    	: constructed(*this), i(j), r(i)
    	{
    
    	}
    
    	thingy(const thingy & rhs)
    	: constructed(*this), r(i)
    	{
    		*this = rhs;
    	}
    
    	thingy &
    	operator = (const thingy & rhs)
    	{
    		i = rhs.i;
    		return *this;
    	}
    
    	friend
    	ostream & 
    	operator << (ostream & os, const thingy & thing)
    	{
    		return os << thing.i;
    	}
    };
    Last edited by Sebastiani; 05-02-2006 at 12:54 AM. Reason: formatting, tabs
    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;
    }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pause/idle thing
    By freedik in forum Windows Programming
    Replies: 13
    Last Post: 08-22-2003, 10:46 AM
  2. A very strange thing
    By gustavosserra in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2003, 01:43 PM
  3. most challenging thing to program
    By volk in forum A Brief History of Cprogramming.com
    Replies: 52
    Last Post: 03-28-2003, 03:56 PM
  4. newbie needs help comprehending simple thing
    By A helpless one in forum C++ Programming
    Replies: 6
    Last Post: 12-16-2002, 09:23 PM
  5. PingPong But how to make 2 thing at the same time..
    By Gugge in forum C Programming
    Replies: 5
    Last Post: 04-02-2002, 06:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21