Declare class object globally but without paramete

2019-07-23 07:00发布

So here's my situation: I have a class foo residing in foo.cpp with header file foo.h included in my main.cpp. I need to be able to declare a foo object as a global variable in main.cpp so that it is available to all functions within main.cpp without having to pass a reference to it every time. The problem is, foo's constructor requires a variable which isn't retrieved from the user until halfway through the main function in main.cpp. I thought I could do this this way:

static foo myFoo;

As a global variable above the main function, then after the necessary data is retrieved from the user (let's call it "variable"), I could call:

myFoo = new foo(variable);

I'm getting an error however with this:

error: no matching function for call to ‘foo::foo()’

referencing the line with static foo myFoo;

So it's basically saying that I'm trying to declare an instance of foo with a constructor taking zero arguments, when there is none.

So my question is: Is there a way to declare a label myFoo out of foo as a global variable so that the program compiles, and then later it can actually be instantiated using the constructor with a variable passed?

I know I could just do something like this:

string temp = "";
static foo myFoo(temp);

above main() and then have some function defined where I could do

myFoo.setVar(variable);

when I needed to. To me this is very ugly and necessitates the inclusion of the function setVar which has no tangible purpose other than to circumvent this very issue. Thoughts?

标签: c++ class
4条回答
一夜七次
2楼-- · 2019-07-23 07:22

One option you have is to make your static instance a pointer:

static foo *myFoo = NULL;

// Later...
myFoo = new foo(variable);

Or you may want to use a default constructor and make an Init method instead.

static foo myFoo;

// Later...
myFoo.Init( variable );

When you're exposing a variable, you don't define it as static. Remove the static keyword and use extern in the header's declaration.

查看更多
Explosion°爆炸
3楼-- · 2019-07-23 07:26

You are indicating that you foo object cannot be properly instantiated until halfway down the main function. This sounds like you will have an object in an inconsistent state for the first half of your main function. Very dangerous. Anyway, there is a couple of ways to get to it: 1) If you want to create a static foo object you can use a default value for your constructor:

class foo {
public:
    foo( type variable = default ) {
        ...
    }
...

You can now declare your foo object as a global in main foo myFoo; and myFoo's variable will be initialised with “default” or, without a default:

class foo {
public:
    foo( type variable ) {
        ...
    }
...

Declare your foo object as a global in main foo myFoo(default);

Depending whether “default” makes sense in your application you can now use your myFoo object in main.cpp's functions. If “default” does not make sense you will have to add a test function before you use myFoo. Of course you also need another member function to be able to set “variable” to your user input (something like set_variable(variable); )

2) Use a pointer. Declare a global foo * myFoo in main.cpp and test if the pointer is NULL before using it. Now your constructor does not need a default value, it still needs the variable: foo::foo(type variable) { ... } You initialise your myFoo object by using: myFoo = new foo(user_input);

You can test the myFoo pointer to NULL as all uninitialised global variables are set to 0 by the startup code.

(As an aside: you do realise of course that globals are frowned upon and as such should be avoided)

Good luck

查看更多
欢心
4楼-- · 2019-07-23 07:27
static foo myFoo;

That line will create a new object of type foo during the static initialization if your program. It will call the default constructor (if you don't define one, the compiler will create one for you in certain situations - you haven't posted enough code to know if that is the case here). Note that if it is defined in a header file, you will need to extern it.

Attempting to set myFoo to a pointer using

myFoo = new foo(variable);

will not compile. new foo(...) returns a foo*, not a foo. If you want it to be a pointer, you need to declare your static variable as

static foo* myFoo;

If you want it to be an object and want to set it to something other than the default, you can implement a copy-assignment operator and do something like this

foo newFoo(variable);
myFoo = newFoo;

Or provide an initialization function to change the construction values after the fact (prefer the copy-assignment in most cases as it will be less prone to errors).

查看更多
太酷不给撩
5楼-- · 2019-07-23 07:35

I believe you might not be defining the default constructor. Once you define a constructor, the default constructor is not automatically defined. This works for me:

myclass.hpp:

#ifndef _MYCLASS_
#define _MYCLASS_
class myClass
{
    public:
        int I;
        myClass(int i);
        myClass();
};
#endif

myclass.cpp:

#include "myclass.hpp"

myClass::myClass(int i) : I(i) {};
myClass::myClass() : I(0) {};

main.cpp:

#include "myclass.hpp"

myClass myGlobalClassObject;

int main()
{
    myClass myLocalClassObject(1);
    myGlobalClassObject.I = 2;

    return 0;
}
查看更多
登录 后发表回答