Click here to Skip to main content
15,892,768 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionbase class operations don't call derived class operations Pin
boogalo7929-May-06 4:13
boogalo7929-May-06 4:13 
AnswerRe: base class operations don't call derived class operations [modified] Pin
toxcct29-May-06 4:49
toxcct29-May-06 4:49 
GeneralRe: base class operations don't call derived class operations [modified] Pin
boogalo7929-May-06 5:01
boogalo7929-May-06 5:01 
GeneralRe: base class operations don't call derived class operations [modified] Pin
toxcct29-May-06 5:04
toxcct29-May-06 5:04 
AnswerRe: base class operations don't call derived class operations Pin
Niklas L29-May-06 5:13
Niklas L29-May-06 5:13 
GeneralRe: base class operations don't call derived class operations Pin
boogalo7929-May-06 5:31
boogalo7929-May-06 5:31 
GeneralRe: base class operations don't call derived class operations Pin
G Haranadh29-May-06 7:46
G Haranadh29-May-06 7:46 
AnswerRe: base class operations don't call derived class operations [modified] Pin
Stephen Hewitt29-May-06 13:52
Stephen Hewitt29-May-06 13:52 
Even after you've implemented toxcct's solution, calling virtual functions in a constructor is bad practice and a recipe for disaster. Consider the following code:
#include "stdafx.h"
#include <iostream>
using namespace std;
 
class Base
{
public:
    Base()
    {
       LogClass();
    }
 
    virtual LogClass()
    {
       cout << "Base" << endl;
    }
};
 
class Derived : public Base
{
public:
    virtual LogClass()
    {
       cout << "Derived" << endl;
    }
};
 
int main(int argc, char* argv[])
{
    Derived d;
    d.LogClass();
 
    return 0;
}


The output of this program is:
Base
Derived


And not, as you may expect:
Derived
Derived


There are languages where the second is produced in code like this (like C# I believe); they've got it wrong. The reason for this behaviour is that the constructor of a class builds the environment the member functions of that class require; they setup the classes’ invariants. For example for a logging class it may open a file and set a member variable to the file handle; all the classes' functions expect it. This means it is wrong to call a classes member functions before its constructor has been called. In our example the class Derived is only partially constructed; the Derived constructor hasn't been called yet so we can't call its member functions; the virtual function call mechanism makes sure this can't happen accidently.


Steve
GeneralRe: base class operations don't call derived class operations [modified] Pin
Member 303984329-May-06 23:07
Member 303984329-May-06 23:07 
QuestionProblem Reciving data in ftp using winsock [modified] Pin
darkcloud.42o29-May-06 3:38
darkcloud.42o29-May-06 3:38 
QuestionInheritance issue [modified] Pin
big_denny_20029-May-06 3:22
big_denny_20029-May-06 3:22 
AnswerRe: Inheritance issue [modified] Pin
toxcct29-May-06 3:26
toxcct29-May-06 3:26 
GeneralRe: Inheritance issue [modified] Pin
big_denny_20029-May-06 3:30
big_denny_20029-May-06 3:30 
GeneralRe: Inheritance issue [modified] Pin
toxcct29-May-06 3:36
toxcct29-May-06 3:36 
GeneralRe: Inheritance issue Pin
Cedric Moonen29-May-06 3:43
Cedric Moonen29-May-06 3:43 
GeneralRe: Inheritance issue Pin
Sarath C29-May-06 3:46
Sarath C29-May-06 3:46 
GeneralRe: Inheritance issue Pin
Sarath C29-May-06 3:45
Sarath C29-May-06 3:45 
AnswerRe: Inheritance issue Pin
Sarath C29-May-06 3:38
Sarath C29-May-06 3:38 
GeneralRe: Inheritance issue Pin
Stephen Hewitt29-May-06 14:32
Stephen Hewitt29-May-06 14:32 
AnswerRe: Inheritance issue [modified] Pin
Stephen Hewitt29-May-06 14:37
Stephen Hewitt29-May-06 14:37 
QuestionContext Menu - Adding an Item in the Windows explorer context menu Pin
Tamal Saha29-May-06 3:11
Tamal Saha29-May-06 3:11 
AnswerRe: Context Menu - Adding an Item in the Windows explorer context menu Pin
toxcct29-May-06 3:16
toxcct29-May-06 3:16 
GeneralRe: Context Menu - Adding an Item in the Windows explorer context menu Pin
Owner drawn29-May-06 17:37
Owner drawn29-May-06 17:37 
GeneralRe: Context Menu - Adding an Item in the Windows explorer context menu Pin
toxcct29-May-06 21:22
toxcct29-May-06 21:22 
GeneralRe: Context Menu - Adding an Item in the Windows explorer context menu Pin
Owner drawn30-May-06 21:12
Owner drawn30-May-06 21:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.