Declaring a static object, but using ctor later?

M

Markus Pitha

Hello,

I have a class "Menu". In this class I declare an object "Controller".
Now I have a problem:
Controller uses a ctor but I get the value I have to pass later in my
program.
The only way to solve this is to make a new instance of "Controller" and
use it dynamically, but how can I use it as static object?

class Menu {

private:
int knotenanzahl;
Controller *controller;
void setMatrix();


public:
Menu();
virtual ~Menu();
void initMenu();
};
--------------------------------------------------------
void Menu::setMatrix() {
..
..
controller = new Controller(knotenanzahl);
..
..
..
}
--------------------------------------------------------

I can't write something like controller(knotenanzahl), because I have to
declare it before I know the value of knotenanzahl.
I need to use Controller in my whole Menu class so declaring it as
static object in the setMatrix method only is no solution.


Markus
 
M

Markus Pitha

Daniel said:
What about introducing a setKnotenAnzahl() into Controller? Or is this
class not in your control or this otherwise impossible?

Yes, I know that this works, but my intention is it to either use
pointers to objects or using static object, because in C++ I'm not sure
when I best use which programming methods like in Java where this issue
doesn't exist.


Markus
 
D

Daniel Kraft

I can't write something like controller(knotenanzahl), because I have to
declare it before I know the value of knotenanzahl.
I need to use Controller in my whole Menu class so declaring it as
static object in the setMatrix method only is no solution.

What about introducing a setKnotenAnzahl() into Controller? Or is this
class not in your control or this otherwise impossible?

If you can't do so, I believe there's no other way than to use a
pointer. But personally, I often use a pointer-to-object as
member-variable, using the class' destructor you can delete it nearly as
easy as using a static object.

Yours,
Daniel Kraft
 
C

cringecoder

Hello,

I have a class "Menu". In this class I declare an object "Controller".
Now I have a problem:
Controller uses a ctor but I get the value I have to pass later in my
program.
The only way to solve this is to make a new instance of "Controller" and
use it dynamically, but how can I use it as static object?

...

I can't write something like controller(knotenanzahl), because I have to
declare it before I know the value of knotenanzahl.
I need to use Controller in my whole Menu class so declaring it as
static object in the setMatrix method only is no solution.

Markus

You could try using a static std::auto_ptr

-DP
 
D

Daniel Kraft

Markus said:
Yes, I know that this works, but my intention is it to either use
pointers to objects or using static object, because in C++ I'm not sure
when I best use which programming methods like in Java where this issue
doesn't exist.

Hm, I believe this is a matter of taste; in general, I prefer static
objects because you do not have to cope with destroying them.
Especially for locals this is useful as this makes exception-safe code
by itself.

For instance fields I use static objects, too, where possible, but in a
case like yours I would not hesitate to use a pointer as the problem
with deleting can be coped with in the destructor nearly as elegant as
not having to delete at all.

Yours,
Daniel
 
D

Dave Rahardja

Hm, I believe this is a matter of taste; in general, I prefer static
objects because you do not have to cope with destroying them.
Especially for locals this is useful as this makes exception-safe code
by itself.

For instance fields I use static objects, too, where possible, but in a
case like yours I would not hesitate to use a pointer as the problem
with deleting can be coped with in the destructor nearly as elegant as
not having to delete at all.

I generally avoid static objects because of this issue:

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12

Here's an example of how this problem can be solved:

#include <cassert>
#include <memory>

namespace
{
std::auto_ptr<Controller> pController;
}

Menu& getController()
{
std::assert(pController.get());
return *pController;
}

void createController(int knotenanzahl)
{
pController.reset(new Controller(knotenanzahl));
}

class Menu {
private:
int knotenanzahl;
Controller& controller;
void setMatrix();


public:
Menu();
virtual ~Menu();
void initMenu();
};
--------------------------------------------------------
namespace
{
Controller& setController(int knotenanzahl)
{
createController(knotenanzahl);
return getController();
}
}

void Menu::setMatrix():
controller(setController(knotenanzahl)
{}
 

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
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top