Thread: Newbe Question - Class constructor

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    39

    Newbe Question - Class constructor

    Code:
    void Polly::SetValues(string Name,string EType,string MinisterOH,string PType,int Noos){
    Name1 = Name;
    EType1 = EType;
    MinisterOH1 = MinisterOH;
    PType1 = PType;
    Noos1 = Noos;
    }
    How can I use the same variable name as whats being passed to the constructor. Ie, instead of calling the private instance variable 'Name1' it can be also called 'Name'.

    Whats happens in the above code when 'Name1' is called 'Name' (same as whats being passed to it) the method that uses it (getName()) and returns the name, returns nothing. It compiles though...

    I know in Java you just say this.Name = Name;

    How is this accomplished in c++? Ps, I hope im not to confusing

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    You can use same concept in c++ also.But in c++ this is a pointer(Don't worry about it if you are newbie).Just remember in c++ for accessing pointers to a class we use arrow notation and for accessing simple objects to a class use dot notation.So you can do it like
    Code:
    this->name=name;

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    39
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  2. Base Class Constructor parameter has a struct
    By cloudy in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2007, 02:24 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  5. Class problem...I think its the constructor.
    By RealityFusion in forum C++ Programming
    Replies: 6
    Last Post: 05-14-2006, 09:02 PM