Using my own method in a constructor...

This is a discussion on Using my own method in a constructor... within the C++ Programming forums, part of the General Programming Boards category; Hi, I currently have a C++ programming assignment where we have been given the header files declaring what the functions ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    Using my own method in a constructor...

    Hi, I currently have a C++ programming assignment where we have been given the header files declaring what the functions look like, and we just have to implement them.

    In the constructor for one of my classes I want to use a method I have written, but is not declared in any of the .h files, i.e. not a method that has been declared by our lecturer. Any ideas for where I can put this method so I can access it, but without altering the .h file?

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Is the method a member of the class?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    11
    No, it is unrelated. I would just add it as a member of the class but they are declared in the .h files and hence I can't.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    If you only need it in one source file, then put it in that source file and use it.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    #include "YourFile.h"

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Or if you're not allowed to add any headers, put it inside the unnamed namespace:
    Code:
    namespace {
    
        void my_method()
        {
        }
    }
    For your small program it doesn't matter, but doing this would avoid clashes with other functions of the same name in other source files.

  7. #7
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    i think he's wondering if he can add a member-function to a class that's already been defined without changing the header. To which the answer would be, yes, use inheritance. Have your own class with whatever extra member functions you need be of the public type declared in your lecturers header.

    Example;

    Code:
    //"lecture.h"
    ......
    class base 
    {
                public:
                     base() {.....};
                     ~base() {......};
                     void memFunc1() const { ....};
                     int memFunc2(int x) {......};
                    .......ect.....
    };
    
    //myclass.cpp
    #include "lecture.h"
    .......
    
    class MyClass : public base 
    {
                public:
                    void myFunc1 () const {....};
    };
    Last edited by Terran; 06-03-2008 at 01:19 PM.
    Sorry, but i'm a Code::Blocks man now.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    That approach cannot be used for a function that will be called by a constructor of the base class, Terran. Order of construction (base class is constructed before derived class) means that a base class constructor cannot make a direct call of a non-static member function (i.e. this->member_function_of_derived_class()).

    More fundamentally, if the function is relevant to implementing your class, why is it being kept in a separate function? The obvious approach would be to place relevant functionality into one of the declared members of the class (eg into the constructor).

    This strikes me as a classic case of having a solution to a problem that happens to violate defined design or implementation constraints. The header file provided AngryStyro's lecturer is such a constraint. The method of introducing the required behaviour to the class needs to comply with that.

  9. #9
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    i missed the part about the constructor.

    agreed!
    Sorry, but i'm a Code::Blocks man now.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    More fundamentally, if the function is relevant to implementing your class, why is it being kept in a separate function? The obvious approach would be to place relevant functionality into one of the declared members of the class (eg into the constructor).
    I am guessing that this function may be something that AngryStyro wants to use to simplify the implementation of the functions in the public interface of the class, so making it a private member function may be ideal. If this function can be written as a non-member non-friend, Daved's unnamed namespace suggestion should work.
    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

  11. #11
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    If you want to use a function 'g' in the constructor of a given class 'A', but 'g' is not declared as a member nor a friend of 'A', why not make 'g' a static function?
    Or is this merely what Daved was saying..

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    If you want to use a function 'g' in the constructor of a given class 'A', but 'g' is not declared as a member nor a friend of 'A', why not make 'g' a static function?
    Or is this merely what Daved was saying..
    No, Daved is saying that 'g' should be a non-member non-friend function in an unnamed namespace, not a static member function (unless by "static function" you actually mean a non-member function, i.e., a free function).
    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

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you meant static non-member, then yes the unnamed namespace is preferred over that.

    If you mean static member, you can't do that without modifying the header, which breaks the original constraint.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by laserlight View Post
    I am guessing that this function may be something that AngryStyro wants to use to simplify the implementation of the functions in the public interface of the class, so making it a private member function may be ideal. If this function can be written as a non-member non-friend, Daved's unnamed namespace suggestion should work.
    I'm not arguing with your general observation, laserlight.

    To say the least, it is relatively rare for even experienced programmers to have a problem like this and even rarer for students, so I'm curious about the specific reason in this case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  2. Windows Service class constructor and OnStart method
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-14-2008, 07:18 AM
  3. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  4. Replies: 3
    Last Post: 03-25-2006, 11:59 PM
  5. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

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