jorgedbucaran
February 19th, 2008, 06:35 PM
Hello,
It seems that C++/CLI automatically calls base class XX constructor when you instantiate a class WW that inherits from XX. Is this the default behavior?
I thought not, but ironically I have had the opposite problem in the past in other languages where I needed to explicitly call the base constructor, like MyBase.New() or base(), etc. So now instead of asking how do I call the base constructor I ask, is there I way to bypass that default behavior so the base constructor does not get called? I imagine there is, however I can't imagine why would I use that, this is why I called this thread Theory Question, it is just to calm down my brain.
To make myself clear, here I have the following three files, Main.cpp with the program main code, BaseClass.h with an inline Class and Class.h with another inline class that inherits from BaseClass:
baseclass.h
#pragma once
ref class BaseClass
{
public:
BaseClass(void)
{
System::Console::WriteLine("ref class BaseClass");
}
};
class.h
#pragma once
#include "BaseClass.h"
ref class Class : public BaseClass
{
public:
Class(void)
{
System::Console::WriteLine("ref class Class : public BaseClass");
}
};
main.cpp
#include "Class.h"
using namespace System;
int main(array<String ^> ^args) {
Class ^instance = gcnew Class();
System::Console::ReadKey();
return 0;
}
The result is this:
ref class BaseClass
ref class Class : public BaseClass
I expected only
ref class Class : public BaseClass
Since I didn't explicitly call the base class constructor. Actually I like this, but I would like to know how to bypass this, just in case.
Thanks,
J
It seems that C++/CLI automatically calls base class XX constructor when you instantiate a class WW that inherits from XX. Is this the default behavior?
I thought not, but ironically I have had the opposite problem in the past in other languages where I needed to explicitly call the base constructor, like MyBase.New() or base(), etc. So now instead of asking how do I call the base constructor I ask, is there I way to bypass that default behavior so the base constructor does not get called? I imagine there is, however I can't imagine why would I use that, this is why I called this thread Theory Question, it is just to calm down my brain.
To make myself clear, here I have the following three files, Main.cpp with the program main code, BaseClass.h with an inline Class and Class.h with another inline class that inherits from BaseClass:
baseclass.h
#pragma once
ref class BaseClass
{
public:
BaseClass(void)
{
System::Console::WriteLine("ref class BaseClass");
}
};
class.h
#pragma once
#include "BaseClass.h"
ref class Class : public BaseClass
{
public:
Class(void)
{
System::Console::WriteLine("ref class Class : public BaseClass");
}
};
main.cpp
#include "Class.h"
using namespace System;
int main(array<String ^> ^args) {
Class ^instance = gcnew Class();
System::Console::ReadKey();
return 0;
}
The result is this:
ref class BaseClass
ref class Class : public BaseClass
I expected only
ref class Class : public BaseClass
Since I didn't explicitly call the base class constructor. Actually I like this, but I would like to know how to bypass this, just in case.
Thanks,
J