C++ and Automatic Variables

Started by
8 comments, last by Nitage 17 years, 7 months ago
How do you declare an automatic variable with C++?

class C
{
    ...
} MyC, *pC;



Is the above correct? I'm doing this so that I can #include a logging class and have instant access to it. EDIT: When I do the above, I always get LNK2005 errors - "[variable] already defined in xxx.obj". Any suggestions? EDIT: The above is when I declare a pointer. class C {} *pC; When I declare a variable I get C2248 errors. ? [Edited by - SouthernMunk on August 25, 2006 5:51:45 AM]
Woooooooot!
Advertisement
You want an external variable, not an automatic variable.
In the header, declare the type and on a separate statement put 'extern C VarName;' then in a cpp file somewhere put 'C VarName;'
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
auto is the default storage specifier in C++. It means that the variable is destroyed at the end of the scope it is defined in. Creating an automatic variable is simple; in any local scope, write:

TheClass theVariableName;

The problem you are encountering stemps from defining multiple global variables with the same name, simply because you included a given definition in several translation units. The correct approach to this is the use of the keywords extern and static. A static variable defined at global scope exists only in the file that defined it. A global variable declared at global scope is not defined, but is expected to be defined as non-static in another translation unit. Thus, at global scope:

static TheClass theVariableName;

Or:

// File.cpp : definitionTheClass theVariableName;// AnotherFile.cpp : declarationextern TheClass theVariableName;

So is the following correct if I wanted a single variable of type C available to all parts of the program that #include "C.h":

// C.hclass C{...};extern C *pC;// C.cppC *pC;// main.cpp#include "C.h"int main(){    pC = new C();    delete pC;    return 0;}
Woooooooot!
Quote:Original post by ToohrVyk
The problem you are encountering stemps from defining multiple global variables with the same name, simply because you included a given definition in several translation units.


OK, but since I use #pragma once and #ifndef's around my header, shouldn't it only be included in the project once, and hence the symbols are only defined once?
Woooooooot!
It is correct but it might be a better Idea to not use a pointer like this :

// C.hclass C{...};extern C MyC;// C.cppC MyC;// main.cpp#include "C.h"int main(){    // No need to create/destroy your pointer    return 0;}


This way you won't have problem with uninitialised pointers and such
Sweet - I think I'll declare the entire class as extern so I don't have two individual statements. That way I won't forget that there's a variable declared by default.

// C.hextern class C{...} *pC;// C.cppC *pC;


Thanks for the help.
Woooooooot!
Quote:Original post by SouthernMunk
Quote:Original post by ToohrVyk
The problem you are encountering stemps from defining multiple global variables with the same name, simply because you included a given definition in several translation units.


OK, but since I use #pragma once and #ifndef's around my header, shouldn't it only be included in the project once, and hence the symbols are only defined once?


pragma once and #ifndef's work on a "per-compilation-unit" basis. That means that they will disallow a same .cpp file to include multiple times a header, but different .cpp files will be able to include the same header. It's logic because each .cpp file needs to see once (and only once) each class defenitions in your header files (else this compilation unit won't be able to use the class).
Quote:Original post by SouthernMunk
Sweet - I think I'll declare the entire class as extern so I don't have two individual statements. That way I won't forget that there's a variable declared by default.

*** Source Snippet Removed ***


Sorry, but that won't work : If you declare the class as extern and define it only in the .cpp file, the compilation units that will include your header file will only see "extern class". They will know that some class exists but they won't be able to actually use any instance of the class because the can't see it's declaration. You should really declare your class normally and use an extern variable.

[EDIT] Woops, you're not declaring the class as extern but only the variable? Then it might work. But why not do two statements? One for class declaration and one for variable declaration?
Quote:Original post by SouthernMunk
Sweet - I think I'll declare the entire class as extern so I don't have two individual statements. That way I won't forget that there's a variable declared by default.

*** Source Snippet Removed ***

Thanks for the help.


That still won't work:

It should look like this:

//.hppclass C{};extern C myC;//.cppC myC;


Declaring variables after the class definition is very uncommon in C++ and there's no need to use pointers here at all.

This topic is closed to new replies.

Advertisement