mystery about global object functions?

Y

ypjofficial

Pls look at this code
////////////////////start///////////
class a
{
public:
a(){
cout<<"inside constructor of a"<<endl;
}

int b()
{
int i=90;
return i;
}
};

a aa;//creating global object of a;
//confusion 1
aa.b();//this will give an compile error

int i = aa.b();//but this will work fine.

void main()
{
}
//////////code ended/////////////

The confusion is
after i create a global object, i can't call globally any of its
function in the standalone expression.Like aa.b();
but the next statement will work fine.(int i = aa.b();)

Why the compiler is behaving like this?
What is the rule here which the compiler is following.?


Thanks and Regards,
Yogesh Joshi.
 
H

Heinz Ozwirk

Pls look at this code
////////////////////start///////////
class a
{
public:
a(){
cout<<"inside constructor of a"<<endl;
}

int b()
{
int i=90;
return i;
}
};

a aa;//creating global object of a;
//confusion 1
aa.b();//this will give an compile error

int i = aa.b();//but this will work fine.

void main()
{
}
//////////code ended/////////////

The confusion is
after i create a global object, i can't call globally any of its
function in the standalone expression.Like aa.b();
but the next statement will work fine.(int i = aa.b();)

int i = aa.b(); is not a statement. It is a variable definition. Definitions
(and declarations) are allowed outside of function definitions, but
statements are not. Statements must always be part of a function definition.
Why the compiler is behaving like this?

It's supposed to behave like this.
What is the rule here which the compiler is following.?

The C++ syntax.

HTH
Heinz
 
Y

ypjofficial

int i = aa.b(); is not a statement.
Ok .agreed.
but i can straight away ignore the value of i and i can achieve the
same thing which i could have achieved with the mere aa.b() statement.
and I can make the compiler a fool.

regards,
Yogesh Joshi
 
R

Robbie Hatley

Pls look at this code
////////////////////start///////////
class a
{
public:
a(){
cout<<"inside constructor of a"<<endl;
}

int b()
{
int i=90;
return i;
}
};

a aa;//creating global object of a;
//confusion 1
aa.b();//this will give an compile error

int i = aa.b();//but this will work fine.

void main()
{
}
//////////code ended/////////////

The confusion is
after i create a global object, i can't call globally any of its
function in the standalone expression.Like aa.b();
but the next statement will work fine.(int i = aa.b();)

Why the compiler is behaving like this?
What is the rule here which the compiler is following?

ISO/IEC 14992, section 3.5, paragraph 1:
A program consists of one or more translation units
linked together. A translation unit consists of a sequence of
declarations.

"aa.b();" is not a declaration, so it's illegal at global
scope.

To put it simply, you can't write "global code" in C++.
Code must be inside functions.


--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
 
H

Howard

Ok .agreed.
but i can straight away ignore the value of i and i can achieve the
same thing which i could have achieved with the mere aa.b() statement.
and I can make the compiler a fool.

regards,
Yogesh Joshi

I doubt the compiler cares what anyone thinks of it. :)

But you are correct (if I understand what you're trying to say), in that
this form of variable initialization allows you to execute code which you'd
otherwise be prevented from doing outside of a function. But C++ *needs*
the ability to do this, or else you'd have to default initialize everything
that was globally declared (as well as static member variable
initializations).

The restriction isn't there because the compiler doesn't want you executing
code from outside of functions, it's there because, aside from variable
initializations, it simply makes no sense to put executable statements
outside of functions. When and in what order would they be executed?

Your method of initializing a variable in order to execute some code can be
useful in some circumstances, where it's really the side-effects of that
initialization that are important to you. But you could also just add those
side effects to the constructor of the object, not to a member function.
Then there'd be no need to declare a "dummy" global variable just to get the
side effects of its initialization.

-Howard
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top