He gave me a programm which doesn't compile and asked the reason which I could not tell him,
have a look at the following snippet,
struct mystruct
{
float foo;
static int bar;//This line generates compilation error.
};
This is a perfectly valid c++ code,and bar can be accessed as mystruct::bar.
And it means that 'bar' will be shared accross all instances of mystruct.
But the scope resolution operator
is in C++ only,concept of name scoping as it exists in C++ was not there.
C is much more closer to machine.Also at the time of C being designed
It was never thought that they may need to share a var across different instances.
Also in C ,struct is not a user defined type instead it is just a collection of heterogenic
items.
So this code is not allowed in C.
Saurabh

No comments:
Post a Comment