C/C++ :: Declaring Map Global Or Wrap In Singleton Class

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


ADVERTISEMENT

C++ :: How To Create A Singleton Class

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

C++ :: Singleton Class For User Settings?

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

C++ :: Singleton Base Class - How To Implement GetInstance Function

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

C++ :: Singleton Class - Auto Seems To Return Wrong Type

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

C++ :: Declaring New Instance Of A Class

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

C++ ::  Declaring Object Of A Class In Another Class

Jul 14, 2014

Information:
I'm using Code::Blocks v12.11.
(I'm using C++/SDL2, but I think that's of no relevance)

Problem:
I create a class named "CSprite" in a "Sprite.hpp"-file. I create a "Sprite.cpp"-file, which includes the "Sprite.hpp"-file. I define the methods of the class "CSprite" in the "Sprite.cpp"-file.

When I try to create an object of "CSprite" in the class named "CPlayer" in the file "Player.hpp" I get an error message. (<-- Looks complicated I know, the code example will be more usefull than this)

Error in the build messages:
C:UsersLinoDocuments1 Data LinoFreizeit1 ProgrammierenC++ & SDL2The Running ManCPlayer.h|30|error: 'CSprite' does not name a type|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 1 seconds) ===|

Code Example:

Sprite.hpp
#ifndef _SPRITE_HPP_
#define _SPRITE_HPP_
class CSprite {

[Code] .....

What did I miss? Did I include the wrong file? Or did I Forget to include the file? Why do I get the error message?

I also tried it with a pointer declaration and the "->" Operator but I got the same error message. I know I could just write a new function to load the texture in my "CPlayer"-class but this would not really answer my question.

View 15 Replies View Related

C++ :: Declaring Array Inside A Class

Apr 18, 2013

class Hallway {
private:
//---------------------------------------------------------------
// DO_04: Declare a Light array of size MAX_LIGHTS
// Hint: look through the methods below to find the name to use
// for the array
//---------------------------------------------------------------

int numLights;
int lights[MAX_LIGHTS];

[Code] .....

I keep getting the error " this.lights[i] is not a struct or class and so you cannot use '.' " on line 34.

How am I supposed to define my lights[] array? I can't modify anything except directly after each comment block.

View 6 Replies View Related

C/C++ :: Declaring Constant Struct As A Member Of Class

Oct 2, 2014

I would like to have a unmodifiable standard of WAVEFORMATEX defined as a member of a class of mine. Something like:

class InputTest {
public:
const WAVEFORMATEX StandardWaveFormat;
public:
void TakeInput(WAVEFORMATEX pFormat);
};

Then in my cpp file to hard-code the values:

WAVEFORMATEX InputTest::StandardWaveFormat {
//Instantiate WaveFormat -- PCM standards
StandardWaveFormat.wFormatTag = WAVE_FORMAT_PCM;
StandardWaveFormat.cbSize = 0; //extra information sent over stream. Usually ignored in PCM format.

[Code] ....

I get the following errors starting with the header file:

Error1error C2146: syntax error : missing ';' before identifier 'StandardWaveFormat'
Error2error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

both associated with the "const WAVEFORMATEX StandardWaveFormat; " line.

Here's a link to the WAVEFORMATEX struct: [URL] .....

Then the cpp source code is probably way off. Let me know if you'd like to see the errors associated with that.

View 11 Replies View Related

C++ :: Private Global Variable To Class?

Sep 30, 2014

I want to make a destructor counter...So it needs to be outside that specific instance of the class. And i want to keep it hidden. I don't want to be able to access it from outside the class...I Don't want main.cpp to have access to this variable

Is there a way to do this?

static int destructorCount;

View 8 Replies View Related

C++ :: Declaring Vector Of Pointer As Private Class Member

Mar 6, 2013

Below is a working program. Please note that the vector <int*> pointer is declared as a public member of the class A.

The problem arises when I try to make it a private (instead of public). First, let's look at the File 1 below. It compiles and works fine.

File 1: main.cpp (working fine)

#include <vector>
#include <iostream>
using namespace std;

[Code].....

View 19 Replies View Related

C++ ::  How To Create Global Object / Instance Of A Class

Aug 25, 2013

I'm new in object oriented programming. I need creating a global object/instance of a class, that can be used by any function.

View 19 Replies View Related

C++ :: Global Class Instance In Multi-File Project

May 3, 2013

I'll just tell you in short how my Problem looks like: I should implement a New Class in a SourceCode i didnt write myself. The source code is extremely sized (i think approx >100.000 Lines of Code), so i dont want to change too much in it in order to get my Implementation done.

MY problem looks simplicified like that: Starting from 3 classes and my new class the pseudo-code looks like that:

Class1(){
float* m_CalibX, m_CalibY;
.. }
Class2(){
char* m_ImageData[];

[Code] .....

So, i need Parameters from 3 different classes to insert in my NewClass. The 3 Classes dont know anyting about each other. So, i need to implement a Class-Instance from Type NewClass which is known by the other 3 Classes. I did solve it in this way:

//ClassInstance.h

#include "NewClass.h"
static NewClass ClassInstance

I just wrote a headerfile with a class-instance which is getting included by the other 3 Classes. So they all know the same Instance and writing their Parameters into it. Is this a decent solution or could it happen to get bugs/ logical mistakes with it?

View 2 Replies View Related

C Sharp :: How To Design / Write Global Config Class That Can Be Inherited

Jan 31, 2013

I need to create a GlobalConfig class. But I want to derive from it in another class.

Here's an example:

public class BaseConfig {
 public string GlobalPath {get; set;}
}  
public class ConfigA :  BaseConfig  {
 public string pathA {get; set;}
}  
public class ConfigB :  ConfigA  {
 public string pathB {get; set;}
}

The idea behind is that I don't want to write the code multiple times and what's more important in class ConfigA I want to set GlobalPath and have access to it in ConfigB.

In other word I want class ConfigB to have a property GlobalPath which was set in class ConfigA.

To clarify I want to have only one object of Config in memory.

When I set BaseConf.GlobalPath to 'A', I want to access it from ConfigB.GlobalPath and also get 'A'.

I always design GlobalConfig as a static class, but static classes can't be inherited. So I tried to implement Singleton Pattern, but ConfigA can't find constructor of class BaseConfig because it's private.

View 1 Replies View Related

C++ :: How To Wrap Numbers Between 0 And 9

Mar 1, 2014

I am writing an encryption program and I want there to be only numbers and letters in the encryption. My offset is going to be a random number between 8 and 15, so I need to figure out how to wrap numbers on the interval [0,9]. I have been playing around with this for a while now and can't seem to get it to work, although it seems like it should be fairly straight forward. Here is what I have been trying:

#include <iostream>
#include <string>
#include <ctime>
#include <stdlib.h>
using namespace std;

[code]...

View 3 Replies View Related

C++ :: How To Make Object Wrap Around The Screen

Apr 2, 2013

How to make an object wrap around the screen?

View 1 Replies View Related

Visual C++ :: Cbutton Text Wrap Not Working With Windows Classic Theme

Oct 17, 2013

I'm programming with VS2010, for windows 7. The user likes to use the windows classic theme, and I discovered that BS_MULTILINE style isn't working as I expect. When a button is pressed in the classic theme, it looks like there is a temporary margin added that causes the text to wrap only when the button is pushed. It works fine with any other theme.

View 1 Replies View Related

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 View Related

C++ :: Constructors In A Singleton

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

C/C++ :: Creating A Singleton Pointer?

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

Visual C++ :: How To Use Watch When Using Singleton

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

C++ :: Singleton Destruction Order

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

C++ :: Out Of Bounds Access (Array / Singleton)

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

C++ :: Singleton Pattern - Delete Pointer Twice

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

C :: Function Name To Wrap Standard Strncpy Function

Nov 5, 2014

I need a function like strncpy; but, want to be sure the destination ends with a ASCII nul char.

So, I wonder what name is used by others in this case.

I have found strlcpy and it looks like a good name choice. [URL] ....

View 5 Replies View Related

C++ :: Singleton Design Pattern - Not All Control Paths Return A Value

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







Copyrights 2005-15 www.BigResource.com, All rights reserved