473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

C++ .NET class member initialization

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
Jul 22 '05 #1
1 2094
"Hans-Christian Stadler" <no**@none.none> wrote in message
news:c5***********@news.cybercity.dk...
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

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: kackson | last post by:
Hi. What does it mean when it is written as: in the test.hpp file-> class MYFUNCTION: public TOP { public: ....some variables... MYFUNCTION(int x, int y, int z.. and some more argument);
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
6
by: Fred | last post by:
Hi I have a class defined in a library that I'd like to add some extra functionality to. This will involve adding a few member variables and a few related methods. As I understand it I can...
2
by: twawsico | last post by:
I ran into this while converting some DX C# code to VB.NET (VS 2003), and I'm just curious as to whether this is intended behavior (and if so, where might I read up on it) or more of a bug. This...
11
by: DrNoose | last post by:
Hi! I've got a program that's almost done, but I'm getting compile errors in two lines: 317 & 319. I think the main error has to do with the Truck Class. I'm a newbie and keep looking at the...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
7
by: BeautifulMind | last post by:
In case of inheritence the order of execution of constructors is in the order of derivation and order of destructor execution is in reverse order of derivation. Is this case also true in case...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
2
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.