473,503 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declaring a static object, but using ctor later?

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
Jun 23 '07 #1
5 1775
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

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress --
so please use good, old E-MAIL!
Jun 23 '07 #2
Daniel Kraft wrote:
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
Jun 23 '07 #3
Markus Pitha wrote:
Daniel Kraft wrote:
>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.
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

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress --
so please use good, old E-MAIL!
Jun 23 '07 #4
On Jun 23, 5:03 am, Markus Pitha <newsgroupsNOS...@pithax.netwrote:
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

Jun 23 '07 #5
On Sat, 23 Jun 2007 13:18:42 +0000, Daniel Kraft <d@domob.euwrote:
>Markus Pitha wrote:
>Daniel Kraft wrote:
>>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.

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-lit...html#faq-10.12

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

#include <cassert>
#include <memory>

namespace
{
std::auto_ptr<ControllerpController;
}

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)
{}
Jun 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1756
by: Eric Liu | last post by:
Hi, Can anyone explain why the following code works? The static object creation is via the private ctor. If I try to define a X object in the mainline, of course I will get complaint about...
4
2191
by: roger | last post by:
Here's a weird one... The code below works just fine when I build in DEBUG mode. Today, I tried to build my solution in RELEASE mode, and immediately fell over this problem - apparently...
16
3131
by: Ed Sutton | last post by:
I use a mutex to disallow starting a second application instance. This did not work in a release build until I made it static member of my MainForm class. In a debug build, first instance got...
15
6717
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
4
2694
by: Johs | last post by:
I have this in a file called bas.h: #ifndef BAS_H_ #define BAS_H_ class GeoObj { public: void draw() const; int getNum();
6
2180
by: VSP | last post by:
Hello, I am just implementing singleton pattern in various ways. In one implementation I created a static member and returning that static member in the getInstance() function. I have made...
6
3216
by: Grey Alien | last post by:
class A { public: A(const B& ref); private: static B& b ; }; How may b be initialized ?
16
2978
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Test { static Test t; static Test init_Test( ) { return t; }
2
1817
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.