Does “static” also provide type?

S

somenath

When I try to compile the following code I get error.
struct Test {
int x;
struct Test t;
};

int main (void) {
}
g++ -ansi -pedantic test.cpp
test.cpp:3:19: error: field ‘t’ has incomplete type

This I can understand. As before the availability of complete “struct Test” declaration I am trying to create an object of that type. So compileris complaining.
But when I change the struct declaration as follows
struct Test {
int x;
static struct Test t;
};
I do not get any error. I am not able to understand what role static is playing here? How compiler is able to obtain the complete definition of struct in presence of static key word?
 
I

Ian Collins

somenath said:
When I try to compile the following code I get error.
struct Test {
int x;
struct Test t;
};

int main (void) {
}
g++ -ansi -pedantic test.cpp
test.cpp:3:19: error: field ‘t’ has incomplete type

This I can understand. As before the availability of complete “struct Test” declaration I am trying to create an object of that type. So compiler is complaining.
But when I change the struct declaration as follows
struct Test {
int x;
static struct Test t;
};
I do not get any error. I am not able to understand what role static is playing here? How compiler is able to obtain the complete definition of struct in presence of static key word?

* Please fix the mess that shite google interface with make of your
relies! *

A class static member object isn't part of an instance the class, so the
compiler doesn't need to know its size. Not to mention the infinite
loop that would occur if it was!
 
T

Thomas Flynn

Ian said:
* Please fix the mess that shite google interface with make of your
relies! *

A class static member object isn't part of an instance the class, so the
compiler doesn't need to know its size. Not to mention the infinite
loop that would occur if it was!

Yea it seems to play the role of a const reference that is shared across
all instances. I wrote this little code to understand:

struct Test {
Test();
Test(int _x);
int x;

const Test &t;
};

static Test * st = new Test(10);

Test::Test(int _x):x(_x),t(*this){} //special constructor for first instance
Test::Test():t(*st){} //normal ctor

Then the code:

Test s;
s.x = 5;
printf("s.x: %d\n",s.x);
printf("s.t.x: %d\n",s.t.t.t.t.x);

Outputs:

5
10
 
M

Marcel Müller

When I try to compile the following code I get error.
struct Test {
int x;
struct Test t;
};

int main (void) {
}
g++ -ansi -pedantic test.cpp
test.cpp:3:19: error: field ‘t’ has incomplete type

This I can understand. As before the availability of complete “struct Test” declaration I am trying to create an object of that type. So compiler is complaining.

Furthermore the size of this object would be infinite since each
instance of it must at least contain another instance. This is an recursion.
But when I change the struct declaration as follows
struct Test {
int x;
static struct Test t;
};
I do not get any error. I am not able to understand what role static is playing here?

The code does not declare any storage. It is only a forward declaration.
Static class members need to be defined in some compilation unit. You
missed that. And so you have n undefined symbol. But since you did not
access Test::t somewhere in the code the linker did not complain.
How compiler is able to obtain the complete definition of struct in presence of static key word?

It is not. You are missing a line of code as mentioned above.

Test Test::t;


Marcel
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top