C++ .NET class member initialization

  • Thread starter Hans-Christian Stadler
  • Start date
H

Hans-Christian Stadler

Hi,

I'm wondering how C++ .NET initializes class members. I have code that
looks aproximately like this:

_gc class SuperClass;

// Factory object creates instances
_gc class Factory {
public:
virtual SuperClass* createInstance() = 0;
};

// Common interface
_gc class SuperClass {
public:
// available class factories
static ArrayList *factories = new ArrayList();

static SuperClass* createInstance(int factory) {
return dynamic_cast<Factory*>
(factories[factory])->createInstance()
}

virtual void doSomething(void) = 0;
};

// Class implementating SuperClass interface
_gc class BaseClass : public SuperClass {
public:
virtual void doSomething(void) {
Console::WriteLine(S"Hello");
}
};

// Factory object for BaseClass
_gc class BaseClassFactory {
private:
static BaseClassFactory *theInstance = new BaseClassFactory();

BaseClassFactory(void) {
SuperClass::factories->Add(this);
}
public:
virtual SuperClass* createInstance() {
return new BaseClass();
}
};

Client code would look like this:

SuperClass *sc = SuperClass::createInstance(0);
sc->doSomething();


The problem is, the BaseClassFactory constructor is never executed!

Hans
 
I

Ioannis Vranos

Hans-Christian Stadler said:
Hi,

I'm wondering how C++ .NET initializes class members. I have code that
looks aproximately like this:

_gc class SuperClass;

// Factory object creates instances
_gc class Factory {
public:
virtual SuperClass* createInstance() = 0;
};

// Common interface
_gc class SuperClass {
public:
// available class factories
static ArrayList *factories = new ArrayList();


What's this. Myself is using .NET i am not programming this way. Why the
call to default constructor (other than the MS manuals use this way).


static SuperClass* createInstance(int factory) {
return dynamic_cast<Factory*>
(factories[factory])->createInstance()
}

virtual void doSomething(void) = 0;
};

// Class implementating SuperClass interface
_gc class BaseClass : public SuperClass {
public:
virtual void doSomething(void) {
Console::WriteLine(S"Hello");
}
};

// Factory object for BaseClass
_gc class BaseClassFactory {
private:
static BaseClassFactory *theInstance = new BaseClassFactory();

BaseClassFactory(void) {
SuperClass::factories->Add(this);
}
public:
virtual SuperClass* createInstance() {
return new BaseClass();
}
};

Client code would look like this:

SuperClass *sc = SuperClass::createInstance(0);
sc->doSomething();


The problem is, the BaseClassFactory constructor is never executed!



The problem is that your code does not compile in VS .NET. Post a code that
compiles followed by your questions. Although .NET API is entirely off topic
in comp.lang.c++. Also try microsoft.public.dotnet.* newsgroups.






Ioannis Vranos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top