Thread: Defining derivated class problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> that's the ONLY consequence of having the declaration there.
    Putting the function definition inside the class definition also has the effect of marking the function as inline. While many optimizing compilers make their own decisions about what to inline, they do take into consideration the programmers suggestions.

    Many people like to have some one line functions (like get and set methods) declared inline in the class definition. However, if you're going to be using a variable of a class that you forward declared, it is best to just make the function definition outside of the class definition.

    Also note that normally these things are broken up in header and source files. For example:
    Code:
    // base.h
    #ifndef BASE_H
    #define BASE_H
    
    class base
    {
    public:
        base() {}
        virtual ~base() {}
        virtual void uselessFunction() = 0; //Just to make it a base class :)
    };
    
    #endif
    Code:
    // class2.h
    #ifndef CLASS2_H
    #define CLASS2_H
    
    #include "base.h"
    
    class class1;
    
    class class2 : public base
    {
    private:
        class1 *ptr;
    public:
        void someMethod();
        void uselessFunction() {}
    };
    
    #endif
    Code:
    // class1.h
    #ifndef CLASS1_H
    #define CLASS1_H
    
    #include "base.h"
    
    class class1 : public base
    {
    public:
        void uselessFunction() {}
    };
    
    #endif
    Code:
    // class2.cpp
    
    #include "class2.h"
    
    void class2::someMethod()
    {
        ptr->uselessFunction();
    }
    Of course you'd probably also have a class1.cpp and a maybe a base.cpp for implementations of the member functions of those classes.
    Last edited by Daved; 08-22-2007 at 03:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Membership Problem
    By josephjah in forum C++ Programming
    Replies: 5
    Last Post: 05-27-2007, 01:48 PM
  2. Inheritance using Stack Class Problem
    By dld333 in forum C++ Programming
    Replies: 17
    Last Post: 12-06-2005, 11:14 PM
  3. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  4. static class problem.
    By Sebastiani in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2002, 03:27 PM
  5. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 05:06 PM