C++ :: Getting Singleton Instance
Oct 25, 2014
Code:
class BoundingBoxTest : public DemoApplication {
public:
#ifdef DYNAMIC_CHARACTER_CONTROLLER
btCharacterControllerInterface* m_character;
[Code] ....
I am creating the object by calling : BoundingBoxTest::Create();
Do I make a getter to retrieve the demo object or do I build on the Create() method... So that it will return the instance if it is not null. and creates one if there is none?
View 3 Replies
ADVERTISEMENT
Apr 6, 2013
I need to implement a singleton, so I've been reading about it online and I'm still not quite sure about all the types of constructors I need to declare:
Code:
class my_singleton {
private:
my_singleton();
my_singleton(my_singleton & X);
my_singleton(const my_singleton & X);
[Code] ....
Is this OK?
View 8 Replies
View Related
Oct 6, 2013
I've tried to program a Singleton class. But the problem is that I don't know how to access the g_pInstance() function. Because this is not working because the constructor and deconstructor is private:
Singleton::g_pInstance()
Code: #include <iostream>
using namespace std;
class Singleton
{
[Code]....
I'm not sure of how to access any object, function, variables in the class when you are using a Singleton. How do you access that?
I'm just asking because I want to know how to do that if I have to use a Singleton sometime when I'm programming.
View 5 Replies
View Related
Jun 26, 2014
I have a class in my application that only needs to be created once, but the object needs to be available to all other classes in my application if necessary. Since declaring everything static can be restrictive (as I understand), I created a class like this:
class Foo {
// Data members
// Constructor/Destructor
// Functions
};
extern Foo* myFoo = new Foo();
And then the global variable gets deleted at the very end of the main method when everything is done:
#include "Foo.cpp" // (yes, I know this is normally bad, this is how I'm required to code)
int main() {
// do stuff
delete myFoo;
return 0;
}
These won't link, though, because I get undefined reference linking errors to myFoo wherever I use it. I'm pretty sure this means I'm creating a singleton wrong, but I'm not sure what I'm doing wrong -- there's no const conflicts and the pointer is properly initialized (to my understanding). If there's a better way to do this than extern, I'm completely open to it, as long as it's understandable and works.
View 9 Replies
View Related
Aug 24, 2014
I have code like this:
Code:
if (S::I().File.isDirectory(arg, S::I().Stat.workingPath)) {
// find out if there is -r option after 1st argument to join files
src_temp.join = true;
S::I().Stat.getRegEx = true;
}
The S is singleton class and I() returns instance of Singleton; there is a Stat object too. When I debug (line by line) I would like to see what values are in the Stat object. Is it possible to do it using Watch panel in Visual Studio 2010?
View 7 Replies
View Related
May 28, 2012
I've been upkeeping a mess of a code recently, that uses "pseudo" singletons. Basically, the current code has "Initialize_All" static functions that initializes all the singletons in a given order. At the end of the program, we call "Destroy_All", and destroy everything in the reverse order.
The code is actually heavilly dll'ed, and Initilize_All and Destory_All are referenced counted. We ask that any client who uses our code call Initialize_All first and then Destroy_All when they are finished. The first Initilize_All will initialize everything, and the last Destory_All will delete everything.
This is showing its limits.
I'd like to move us to a fully singleton design. The singleton pattern means we don't have to use an Initilize_All, and each singleton can manage construction dependencies by itself (we are mono-threaded).
Each singleton is "clean", so it is cleans itself up at dll destruction.
The big question is this one:
If there is a singleton dependency during destruction, eg: ~A requires an instance of singleton B (which is in another DLL), are we guaranteed proper behavior?
Or, is there an "Static de-initialization order fiasco"?
If yes, are there any design that can combat this fiasco, short of having each singleton register itself in a manager, that will destroy them in reverse order?
View 6 Replies
View Related
Oct 11, 2014
My current idea of how to work with user settings goes like this:
1. Create a class to hold all of the user settings.
2. Use that class to load/save/hold settings in memory.
3. Create an instance of that class once in the entry point of the program (int Main or whatever).
4. Pass, by reference this same class instance around to all of the other classes that need the user settings.
5. Once all other objects deleted, save and then delete the User Settings class.
I created a psuedo-code example below of this. My question is if this is the best way or should I be doing something else. In particular, I am wondering if somehow I can avoid passing the settings class by reference all of the time. Would this be a good case scenario for a "Singleton" type class?
#include <string>
class UserSettings {
private:
std::string SettingOne;
int SettingTwo;
bool SettingThree:
[Code] ....
View 9 Replies
View Related
Feb 3, 2014
#include <iostream>
int main() {
int bit = 1;
int init = 0xf ^ (1 << bit);
char* c = new char(2);
sprintf(c, "%x", init);
std::string initVal = std::string("4'h") + c;
std::cout << initVal << std::endl;
}
Above code is compiling as I expect it to be.
Problem is when I run it, it prompts me the following message:
Out-of-bounds access (ARRAY_VS_SINGLETON). Passing "c" to function "operator +(HSTString const &, char const *)" which uses it as an array. This might corrupt or misinterpret adjacent memory locations.
View 2 Replies
View Related
Feb 22, 2012
environment : qt creator 4.7
code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
class singleTon {
[Code] ....
this is a singleton pattern first,it doesn't matter, why I could delete this pointer twice?because the gcc compiler?That mean in the surface, "delete pInstance1;" this movement just mark the memory pInstance1 has been deleted but not real?does any one encounter this phenomenon?
View 7 Replies
View Related
Jul 13, 2014
#include <map>
#include <iostream>
#include <stdexcept>
[Code]....
SuperSmartPointer<int> ptr4((int*)ch); this line gives error error as double deletion will occur.
Solution :
1) make the reference map global variable.
2) wrap the map in a non-template singleton class.
View 1 Replies
View Related
Aug 20, 2014
I'm playing with the idea of a singleton base class, but I'm having an issue with how to implement the GetInstance() function in the base class. Since I'm trying to make this ridiculously simple for the child, I'd like to handle that in the base.
class Singleton {
private:
static Singleton* instance;
Singleton() { Construct(); } // Private to avoid other instances
[Code] .....
It would be easy to use like so:
class Hello : public Singleton {
private:
std::string hello;
void Construct() { hello = "hello"; }
public:
std::string GetHello() const { return hello; }
};
Then the instance would be handled like so:
std::cout << Hello::GetInstance()->GetHello();
View 12 Replies
View Related
Jul 18, 2013
I am trying out a technique for a singleton class:
// access controlled singleton, accessed through function "instance()"
// singleton is constructed in this function
// so that constructor and destructor will be used
class single {
// private constructor/destructor
[Code] .....
Playing around with the code in main(), I am having trouble with auto:
single& s = single::instance(); // works fine
auto a = single::instance(); // error ~single() is private
When I make the destructor public, the output of the program is:
ctor
dtor
dtor
So I fixed this by typing auto&. I'm still confused though, why wouldn't auto know I am returning a reference?
View 2 Replies
View Related
Jan 17, 2013
I have the following code where I use the singleton design pattern, but I get the warning:
warning C4715: 'CM::Instance' : not all control paths return a value
Code:
CM& CM::Instance() {
DWORD dwWaitResult = WaitForSingleObject(mutex, INFINITE);
switch(dwWaitResult) {
case WAIT_OBJECT_0:
[Code] ....
How can I fix this warning?
View 4 Replies
View Related
Jul 9, 2014
I was going through Singleton design pattern and get to know that objects can be created only by static function of that class and constructors are make private.
My question is, why assignment operators are not made private through which we can create a copy of already existing object.
I tried below code and assignment works, so I have new object sc3. I know that its referring to memory of sc1 but finally I was able to create object without using static function.
Also, why copy constructor not made as private.
Below is code:
#include <iostream>
using namespace std;
class Singleton {
private:
static bool instanceFlag;
[Code] .....
View 3 Replies
View Related
Oct 4, 2014
I'm currently learning the Qt framework and doing my first tutorial. Straight away I saw something that baffled me:
notepad.h
Code: namespace Ui {
class Notepad;
}
class Notepad : public QMainWindow
[Code] ....
Note the ui pointer and the heap allocation in the class constructor; I can't wrap my head around why one would do this. What's going on here?
View 5 Replies
View Related
Nov 6, 2014
I am putting a instance o the Vehicle Class inside the constructor of the Calculate Class then calling it later. I get a warning saying the variable is not used and a error when I try to used the functions from the vehicle class saying use of undeclared identifier.
Code:
#include <iostream>
#include "Calculate.h"
#include "Vehicle.h"
#include <fstream>
Calculate::Calculate(){
[Code] ....
View 9 Replies
View Related
Sep 11, 2014
can i have 1 static variable with instance different value?
View 3 Replies
View Related
Feb 12, 2014
Okay so I have a class Student, which takes a number and a vector as a parameter for the constructor. Everything works well, until I output the values of the vector for every instance. The problem is that the same vector is being shared with EVERY instance I create, but I want it to be unique for every single one!
//Student.h
#ifndef __Grade_calculator__Student__
#define __Grade_calculator__Student__
#include <iostream>
[Code].....
View 1 Replies
View Related
Nov 24, 2012
I was wondering if (in C++) you can instantiate a class (class foo) then have said class return the already instantiated object. (foo::instance())
In other words, can I have a class return it's-self via it's own methods? I want to be able to create a class (i.e. class foo) early in my program so it is already setup and ready to go. Then, farther down the line, I want to be able to call functions from that class without having to pass that object as an argument to my calling function. Can I do something like so:
MyClass::ReturnSelf()->foo();
or
MyClass::ReturnSelf().foo();
View 13 Replies
View Related
Mar 2, 2014
I have a background in c# and am very frustrated with c++. If I created a class in c# like so:
public class Memory{
int x = 0;
int y = 0;
int height = 0;
int width = 0;
string firstname = "Bob";
string lastame = "Chester";
}
and then created a new instance of this class from a separate class by doing:
public class Main{
Memory mem = new Memory();
}
I have raked the internet for a way to create a new instance of a class in c++ while keeping its default values and have come up empty handed.
View 13 Replies
View Related
Dec 2, 2011
Code:
public class ColorGenetics
{
public static hk1Class jw;
public static linkedClass link;
}
[code]...
Code builds fine but compiler says I tried to use bject link w/o providing an instance. Class linkedClass has an instance of Class hk1Class set to var jw1.I had linkedClass useing the same var jw for the object instance. I changed it to jw1 thinking that would clear it up and it didn't.
View 2 Replies
View Related
Jan 17, 2014
I know that it is possible to pass a class instance to a function, but in my experience, if said function changes any variables of the class, they don't actually get changed. For example, we have class object, that has a member int number = 5. Lets say we have two functions, func1() and func2, which are not members of class object. If we pass object to func1() which, lets say, increases number by 5 (so now number = 10), at the end of that function number still = 5.
Is there a way to bypass this and have functions alter class variables permanently?
I know that I can pass variables by reference, but, in my experience, such a thing does not work with vectors (which I am also dealing with), so simple passing the desired variables by reference won't work.
View 5 Replies
View Related
Sep 4, 2013
I'm trying to learn as much C++ as I can. I was writing a program that mixes linked lists and classes. There is the class "Obj" which only holds an integer called 'data' and the classic "struct node" structure for linked list, but this time the "node" structure will hold an instance of "Obj" Class and the next* pointer.
#include <iostream>
using namespace std;
class Obj {
private:
int data;
public:
[code]....
View 2 Replies
View Related
May 22, 2013
So I have a class that is like this:
class card {
public:
int id;
int val;
};
card card1;
card1.id = 1;
card1.val = 2;
card card2;
card2.id = 2;
card2.val = 45;
etc...
So my question is firstly, is there a better way to implement this? (a vector of classes or something maybe?) and how can I call up a specific instance of the class. For example, if I want the val of a specific instance of the class, how best can I do that?
View 3 Replies
View Related
May 6, 2014
I'm trying to change the values of some instance variables in my Controller Class so that when the user inserts values into main class it changes for Controller.
class Controller {
public:
Controller();
~Controller();
double PCFreq;
__int64 CounterStart;
[Code] ....
The user should be able to choose which foo the want to use. So I create an object of controller in main like this
Controller* con = new Controller()
Now my issues is, when I take user input (an integer) and try to do this
con->choice1 = choice1;
only the object of con's choice1 is = to user input.
However back at the class for Controller, choice1 hasn't received a value.
I can't initialize through Controllers constructor because I get the user input through a switch statement and the range of con would only be as far as the case.
View 2 Replies
View Related
Feb 25, 2014
I need to send same instance of object of a class in two function (Main function and thread function). The class is something like this:
//The class need to have constructor.
Class ABC {
public:
DWORD *IdG;
ABC(int number) {
IdG = new DWORD[number];
}
}obj(32);
The obj(32) is called in following two function. Function F1 is called using thread in main function.
void F1() {
obj.test;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
obj.test1;
_beginthread(F1,0,(void*)number);
}
The code works well when the object of class ABC is created as shown above. My problem is the value that is passed in the object ('32') is to be read from the file.
If I read the file and create object separately in Main function and function 'F1' then , function 'F1' is not executed.
How to create same instance of object for Main function and function 'F1' with value passed in the object taken from the file.
View 1 Replies
View Related