473,394 Members | 1,748 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,394 software developers and data experts.

instance scope ?

Hi.

Consider the case of a class that has some methods that don't require
arguments and not to handle private variables; as example see this
openGL function to draw a square:

void cgutils::drawSquare(float x, float y, float width, float height,
float r, float g, float b, float alpha)
{
glColor4f(r, g, b, alpha);
glBegin (GL_LINE_LOOP);
glVertex3f (x, y, 0.0);
glVertex3f (x+width, y, 0.0);
glVertex3f (x+width, y+height, 0.0);
glVertex3f (x, y+height, 0.0);
glEnd ();
}

I need to call this function in various project files.

Declaring an instance like:

myclass name;

the scope of instance is internally the file where is declared?

Actually, in each file, after the include statements, I declare
a new class instance, using each time a different name.
So if I must use the drawSquare method in 5 project files, I declare 5
instances of class, with 5 different names...

Surely this is not the best way...

At least, what if I use the same name in each file (to have a more clear
code...) ?? Maybe this can create conflict/problems?

thx,

Manuel

Jan 19 '06 #1
4 1659
Manuel wrote:
Consider the case of a class that has some methods that don't require
arguments and not to handle private variables; as example see this
openGL function to draw a square:

void cgutils::drawSquare(float x, float y, float width, float height,
float r, float g, float b, float alpha)
{
glColor4f(r, g, b, alpha);
glBegin (GL_LINE_LOOP);
glVertex3f (x, y, 0.0);
glVertex3f (x+width, y, 0.0);
glVertex3f (x+width, y+height, 0.0);
glVertex3f (x, y+height, 0.0);
glEnd ();
}

I need to call this function in various project files.

Declaring an instance like:

myclass name;
What's "myclass"? Is it related in any way to "cgutils"?
the scope of instance is internally the file where is declared?
If you declared it outside of any function (IOW, any other scope), then
yes, it's the file.
Actually, in each file, after the include statements, I declare
a new class instance, using each time a different name.
OK. No problem so far.
So if I must use the drawSquare method in 5 project files, I declare 5
instances of class, with 5 different names...
OK. If you say so.
Surely this is not the best way...

At least, what if I use the same name in each file (to have a more clear
code...) ?? Maybe this can create conflict/problems?


Since this function has nothing really to do with the _object_ for which
it is called, it's probably better to declare it 'static' and call with
the class name instead of creating instances. Read up on static members.

V
Jan 19 '06 #2
Victor Bazarov wrote:
What's "myclass"? Is it related in any way to "cgutils"?
Yes..it's the same thing. Initially I want write a generic question, but
writing I've used the particular case...sorry.


Since this function has nothing really to do with the _object_ for which
it is called, it's probably better to declare it 'static' and call with
the class name instead of creating instances. Read up on static members.

Thanks,

Manuel
Jan 19 '06 #3
"Manuel" <ma**********************@tin.it> wrote in message
news:43**********************@reader4.news.tin.it. ..
Victor Bazarov wrote:
What's "myclass"? Is it related in any way to "cgutils"?


Yes..it's the same thing. Initially I want write a generic question, but
writing I've used the particular case...sorry.


Since this function has nothing really to do with the _object_ for which
it is called, it's probably better to declare it 'static' and call with
the class name instead of creating instances. Read up on static members.

Thanks,

Manuel


Victor's answer is the right answer, but to answer your deeper question, use
the extern keyword to say that the object is inside some other source file
(not the technical term, btw).

For instance.

MyFile.cpp:
MyClass MyInstance;

MyHeader.h:
extern MyClass MyInstance;

Now, if you include MyHeader.h in another .cpp file the .cpp file be able to
use the same instance of MyInstance as the one in MyFile.cpp since it's
declared extern (external) meaning the instance is found external to that
source file.

Maybe not all the right technical terms, but that's what it does.
Jan 21 '06 #4
Jim Langston wrote:

Victor's answer is the right answer, but to answer your deeper question, use
the extern keyword to say that the object is inside some other source file
(not the technical term, btw).


I believe the term you're looking for is "translation unit".
Jan 22 '06 #5

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

Similar topics

34
by: SeeBelow | last post by:
I see the value of a class when two or more instances will be created, but Python programmers regularly use a class when there will only be one instance. What is the benefit of this? It has a...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
15
by: dutchgoldtony | last post by:
Hi all, I was just wondering if this is possible. I'm trying to implement a viterbi decoder in C and am creating an array of nodes (the struct), and an array of pointers to nodes (the member...
4
by: Ritesh Raj Sarraf | last post by:
Hi, I have a class defined in a file called foo.py In bar.py I've imported foo.py In bar.py's main function, I instantiate the class as follows: log = foo.log(x, y, z) Now in main I'm...
4
by: John Mishefske | last post by:
I have a "Tracker" class that registers "Messenger" classes to allow distribution of unicast and broadcast messages between instances. In code a reference to the Tracker class instance is...
6
by: jimbo | last post by:
Dear all, I am more or less new to c++ programing and therefore still have problems with some fundamentals :( At the moment I try to build a GUI-Application with Qt4. Sometimes I have seen in...
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
1
by: vituko | last post by:
Hi, I have another problem migrating to php5. An important piece of my "web kernel" is a system of include / require throw exceptions : Instead of : require $x I do : trigger_error...
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.