473,397 Members | 2,056 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,397 software developers and data experts.

mystery about global object functions?

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.

Jul 23 '05 #1
4 1413
<yp*********@indiatimes.com> schrieb im Newsbeitrag
news:11**********************@o13g2000cwo.googlegr oups.com...
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
Jul 23 '05 #2
>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

Jul 23 '05 #3
<yp*********@indiatimes.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #4

<yp*********@indiatimes.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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


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

Jul 23 '05 #5

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

Similar topics

88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
6
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
10
by: David P. Donahue | last post by:
When I wrote websites in VB .NET, I would often put functions in Global for all the pages to call. Now, in C#, doing so results in "references to non-static objects" and whatnot. I realize what...
4
by: Simon Harris | last post by:
Hi All, I'm new to ASP.Net, so be gentle! (Plenty of 'classic' ASP experience), just one question... - Am I correct in thinking that global functions are stored in ASCX files? Thanks! ...
6
by: Maarten | last post by:
Here is a nice problem. I have two scripts, one calling the other with via an include. The script that is included contains a variable, a function and a call to that function. The variable needs to...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.