thing

This is a discussion on thing within the C++ Programming forums, part of the General Programming Boards category; I think this is a silly question to ask but ... is there any way to use the constructers (overloaded ...

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262

    thing

    I think this is a silly question to ask but ... is there any way to use the constructers (overloaded or default) as the setters for a class?

    here is some code:

    Code:
    #include <iostream>
    using namespace std;
    
    
    
    class thing
    {
    protected:
    	int number;
    
    public:
    	thing();
    	thing(int newNum);
    	void SetNum ( int newNum );
    };
    
    
    
    thing::thing()
    {
    	number = 42;
    }
    thing::thing(int newNum)
    {
    	number = newNum;
    }
    
    
    
    
    void thing::SetNum( int newNum )
    {
    // want to use thing(); or thing(int newNum) here
    }
    
    
    
    int main()
    {
    	thing things(2);
    	things.SetNum(1);
    
    	return 0;
    }


    obviously this is a silly example, but I have a bigger class which has about 5 or 6 constructers, and it needs the same amount of setters ... I could just copy and paste, in fact i have done so, but I was hoping there is a way to use the constructers in setters.

    Does anyone know how to do this? Is it possible?
    Last edited by twomers; 05-01-2006 at 10:36 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    Create a private function which does the bulk of the work, which you can call from either the constructor or other member functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    What if you use the setter in your constructors instead? Though in that case, you may miss out on being able to use a constructor initialisation list.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Constructors are the setters for the initial value of the object members. But once the object is constructed, the constuctors are meaningless. If appropriate, there is nothing wrong with copy/paste the body of a constructor into a setter, but once constructed, any changes to the member values needs to be done with setters---or destructor, of course.
    You're only born perfect.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    Obviously NIALL you use the setter to overload the constructor as i have been saying to you, god to get the person next to you to listen you have to go on a forum ....

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    void thing::SetNum( int newNum )
    {
       * this = thing(newNum);
    // want to use thing(); or thing(int newNum) here
    }
    Kurt

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    It worked Niall says thanks

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Thanks Zuk, and Salem, and Valt3n, and elad, and laserlight, but mainly not Valt3n!

    I like the *this idea though! Cheers again!

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    Thats not very nice now is it? This means war,

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Brendan shutup and leave everyone alone!

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Sorry about him people ... however, I should also appologise for the title of the post - thing was a test name to see if the colour code thing worked, and I forgot to change it

  12. #12
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Valt3n is Twomers alter ego - he's having an identity crisis...
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I like the *this idea though!

    That's a cute tidbit, but you shouldn't actually use it in real code.

  14. #14
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Quote Originally Posted by Daved
    >> I like the *this idea though!

    That's a cute tidbit, but you shouldn't actually use it in real code.

    Why's that?

  15. #15
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Could't be less efficient.
    Kurt

Page 1 of 2 12 LastLast
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, 09:46 AM
  2. A very strange thing
    By gustavosserra in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2003, 12: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, 02:56 PM
  4. newbie needs help comprehending simple thing
    By A helpless one in forum C++ Programming
    Replies: 6
    Last Post: 12-16-2002, 08: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, 05:13 PM

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