|
|
View previous topic - View next topic |
Author |
Message |
Jihgfed Pumpkinhead Stephen Hawking
Joined: 21 Jan 2003 Posts: 259 Location: Toronto, Canada
|
Posted: Mon Jul 21, 2003 4:46 am Post subject: Invisible Instances of Classes |
[quote] |
|
Code: | void errMsg ( string msg ) {
MsgBox ( msg ); // line 1
MsgBox msgBox ( msg ); // line 2
} |
In the following, line 2 works fine, but line 1 doesn't work at all. In fact, I'm getting an odd error message I've never seen before:
MingW v. 3.2 wrote: | : declaration of 'msg' shadows a parameter
: no matching function for call to 'MsgBox::MsgBox()'
: candidates are:
MsgBox::MsgBox(const MsgBox&)
MsgBox::MsgBox(std::basic_string<char, std::char_traits<char>, std::allocator<char> >) |
Borland v. 5.5 wrote: | : Multiple declaration for 'msg' in function errMsg(string)
: Could not find a match for 'MsgBox::MsgBox()' in function errMsg(string) |
Both of these errors occur at line 1, and I get no errors for line 2. Am I not allowed to create an "invisible" instance? I seem to remember being able to do this before with a class very similar to this one, and I know I do something similar with "string" all the time.
MsgBox is a class with one constructor, which constructor takes a single string, as you've probably guessed.
Thanks a lot for any help. This has me mighty confuzzled; I can certainly get by without an answer, I'll just make sure to name all my class instances, it's just that I'm almost certain that I was able to do this exact same thing in a previous program, and it's driving me crazy.
|
|
Back to top |
|
|
LeoDraco Demon Hunter
Joined: 24 Jun 2003 Posts: 584 Location: Riverside, South Cali
|
Posted: Mon Jul 21, 2003 5:11 am Post subject: |
[quote] |
|
I've never heard of an "invisible instance". (Which is not to say that such a thing does not exist. Just that I have not heard of it.) In c++, you need a handle of memory that is allocated, otherwise you start wasting memory on your system's memory heap (as in, you have the program allocate memory, and then you never free it).
What you might be thinking of is the way pointers and new works:
Code: | Foo *foo = new Foo( bar ); |
In which the constructor you wish to use is directly called. Or, you might be thinking of static methods:
Code: | Foo::foo( bar ); // where foo is static, and you don't have an instance of Foo anywhere |
Although, I have never seen a static constructor (I'm not sure you could actually write one in C++), so that probably wouldn't be the way to go.
If you wanted a single "invisible instance" of Foo, you could always make it a Singleton; Singleton's store a pointer to an instance of a class (usually themselves; they aren't really templated or anything. Although, you might be able to do that.); if a client asks the Singleton for an instance of itself, it [the Singleton] returns the instance it has. (Should that pointer be null, it creates one.) In this case, you could have a sort of global Singleton Foo, which you could use in a variety of ways. I don't think this would work especially well for what you seem to be doing, however.
(And, as to what your compiler was thinking you were trying to do: it (most likely) was trying to redefine msg to be of type MsgBox, but confused itself.) _________________ "...LeoDraco is a pompus git..." -- Mandrake
|
|
Back to top |
|
|
Jihgfed Pumpkinhead Stephen Hawking
Joined: 21 Jan 2003 Posts: 259 Location: Toronto, Canada
|
Posted: Mon Jul 21, 2003 6:12 am Post subject: Fixed! Thanks. |
[quote] |
|
You're right about what the compiler was thinking, it must have been trying to do some strange conversion or assignment or something; thanks a lot. All I had to do was disambiguate my code a bit. Now I have:
Code: | void errMsg ( string msg ) {
MsgBox ( "Error:\n" + msg );
} |
which works fine.
I'm certainly not sure what the terminology for this is, though. "Invisible instance" is just a phrase I cooked up to describe it. Sorry, you'll have to forgive my rough and unschooled patois. Heh.
Thanks for the help.
|
|
Back to top |
|
|
Rainer Deyke Demon Hunter
Joined: 05 Jun 2002 Posts: 672
|
Posted: Mon Jul 21, 2003 4:10 pm Post subject: Re: Fixed! Thanks. |
[quote] |
|
Jihgfed Pumpkinhead wrote: |
I'm certainly not sure what the terminology for this is, though. "Invisible instance" is just a phrase I cooked up to describe it. Sorry, you'll have to forgive my rough and unschooled patois. Heh. |
It's called a "temporary object". Temporary objects are allocated on the stack and automatically cleaned up just like local variables. Temporary objects are ubiquitous in C++. For example:
The expression 1 creates a temporary object of type int with value 1, and the expression 2 creates a temporary object of type int with value 2. Then the expression 1 + 2 adds the values of the two temporary objects and stores the result in yet another temporary object of type int. Finally the newly created variable a is initialized with the value from the third temporary object and all the temporary objects are destroyed (in reverse order of creation). Just about any expression that is not the name of a variable can create temporary objects, although in some cases the compiler is allowed to remove extraneous temporary objects.
|
|
Back to top |
|
|
LeoDraco Demon Hunter
Joined: 24 Jun 2003 Posts: 584 Location: Riverside, South Cali
|
Posted: Mon Jul 21, 2003 5:53 pm Post subject: Re: Fixed! Thanks. |
[quote] |
|
Rainer Deyke wrote: | It's called a "temporary object". Temporary objects are allocated on the stack and automatically cleaned up just like local variables. Temporary objects are ubiquitous in C++. |
D'oh! I should have known that. For a compiler I wrote for one of my courses, the thing generated a large amount of temporaries. Heh. I've just never seen temps used in that specific way. _________________ "...LeoDraco is a pompus git..." -- Mandrake
|
|
Back to top |
|
|
|
Page 1 of 1 |
All times are GMT
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|