instance scope ?

M

Manuel

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
 
V

Victor Bazarov

Manuel said:
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
 
M

Manuel

Victor said:
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
 
J

Jim Langston

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




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.
 
R

red floyd

Jim said:
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".
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top