How to make static constructor

Dear all,i'm new here..Nice to meet you all..I need help and guidance from all of you about C++ programming. Anyway, my problem now is, i don't know how to change or make this below program become a static constructor...

class TestData
{
public:
static int data;
TestData();
~TestData();

};


int TestData::data = 1;

TestData* dataValue;

TestData :: TestData()
{
//monprintf("Constructor for TestData!\r\n");
dataValue = this;
monprintf("Data Value = %d\r\n",dataValue);
}

main()
{
TestData a,b,c;

while(1);
}

=====================================
Output -->
Data Value = 148918396
Data Value = 148918392
Data Value = 148918388
=====================================

Is it if i change to static constructor, i will get the same value for all output?..
Your attention and help is much appreciated.
You are using void main, which is a crime.
In addition, you need code tags around that inexplicable travesty.
Now then, I believe you are printing the address of the pointer and not its contents. I'm not sure you can even define a constructor as static.
And I hope you don't plan on referring to a global in a constructor in any intelligent code. Frankly, I have difficulty reading that code, what with that ridiculous global pointer, so I may be wrong.
Last edited on
Dear tummychow,

Yes, it's correct, i'm trying to print address not the content..Actually, this is 1st time i write the code about constructor and 'this' pointer..
May you give an example how can i write the static constructor to include 'this' pointer and how actually 'this' pointer execute..?..

Your help and attention is much appreciated..:)
closed account (1yR4jE8b)
Static Constructors are a C# mechanism, they don't exist in C++. The term Static Constructor is a bit misleading as well, since a Constructor needs an object while static functions are not tied to an object.
this is just a pointer to the class object from which a function is called. When you are defining a member function (a "method" as they are also known), the pointer this is also defined. It points to the class object from which the member was called.
And I thought as much with static constructors. To be honest, I don't see how static constructors could exist (at least not in c++, and I don't really see the purpose in C# either, but I don't program C# so I can't say), because a static (in class terminology) is specifically not bound to the class in question.
Thanks so much for darkestfright and tummychow...
anyone can help me for this problem..?

how to define one of the member functions in my class as static? and call the 'this' pointer from that function?

Your help and attention is much appreciated..:)
static functions don't have a this pointer. That's what makes them static.
owh..thanks a lot Disch...:)
A weird thing about C++ constructors - is that although you cannot use the static keyword
on the function - you can call them in code as if they were static - which they are in a kind of way.
Dear guestgulkan,

May you explain me more?
Your help and attention is much appreciated. Thank you..:)
you can call them in code as if they were static
Not really. Once a constructor is called, the object has already been allocated. It's why you can actually use it.
Also, you don't (usually) call constructors.
You can do this:

1
2
3
4
5
6
7
8
9
10
int main()
{
    MyClass mc = MyClass::MyClass() //use  default or other constructor

    //or create an unamed variable as you sometimes do for function parameter passing
    
    function(MyClass () ); //like this
    function (MyClass::MyClass() ); //or like this
   
}


The condtructor has to be the one you wrote youself.
Last edited on
Topic archived. No new replies allowed.