help in classes

This is a discussion on help in classes within the C++ Programming forums, part of the General Programming Boards category; Hi. I am stuck in the following problem and I am hoping that if anyone can help me in this. ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    help in classes

    Hi.
    I am stuck in the following problem and I am hoping that if anyone can help me in this.
    So imagine a class..


    Code:
    Class myClass{
    func1(params );
    func2(params);
    private:
    ---state vars--
    }
    Now.. lets say in definitations, I want to use the func2 definition in func1..
    how do i use it?
    Code:
    myClass::func1()
    {
    
    //needs to call func2() here..
    
    }
    It would nice if anyone can guide me in this..
    Thanks a lot for help and suggestion.
    best regards

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,905
    Since both functions are part of the same class you would call it just like a global function.
    Code:
    myClass::func1()
    {
        funct2();   // call func2() here..
    
    }
    Jim

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    In the position you've written it..The ""---state vars--"" would already be private...
    and in
    Code:
    //needs to call func2() here..
    I've recently heard that
    Code:
    this->func2(..params..);
    is a better practice than;
    Code:
    func2(..params..);
    Is it true...if yes..why?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,905
    In my opinion I don't think it is a better practice, to me it just adds clutter to the function.

    Jim
    Last edited by jimblumberg; 05-10-2011 at 08:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversions between base classes and derived classes
    By tharnier in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2011, 11:50 AM
  2. Classes access other classes local variables
    By parad0x13 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2010, 04:36 AM
  3. Classes with Other Classes as Member Data
    By njd in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2005, 10:30 AM
  4. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 03:46 PM
  5. Classes inside Classes
    By LostGeneration in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2003, 12:02 PM

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