Visual C++ :: Static Class Member In Multithreaded Environment

Nov 21, 2014

I have a class having static member.I have get and set methods which will Get and Set Values to this variable. In a multithreaded application does it have any thread safety issues.

Class a {
static int b;
void Set (int c);
int Get();
};

View 10 Replies


ADVERTISEMENT

C++ :: Debug Multithreaded Application In Unix Environment?

Mar 3, 2013

How can we debug the multithreaded application in unix environment?

View 1 Replies View Related

C++ :: How To Initialize Static Member Of Class With Template And Type Of Nested Class

Oct 7, 2014

How to initialize a static member of a class with template, which type is related to a nested class?

This code works (without nested class):

#include<iostream>
using namespace std;
struct B{
B(){cout<<"here"<<endl;}
};
template<typename Z>

[Code] ,....

View 1 Replies View Related

C++ :: Function Pointer To Non-static Class Member

Aug 19, 2014

I have the following problem: I am using NLOpt for optimization. The API provides functions to set the objective. This is done as follows:

double objective(const vector<double> &x, vector<double> &grad, void *data)
{
return x[1]*x[0];
}
int main(){
nlopt::opt opti(nlopt::LD_MMA,2);
opti.set_min_objective(objective,NULL);
vector<double> x(2);

[Code]....

Now I want to make the function objective a member of a class:

class Foo {
public:
double objective(...){..}
};

How can I give this method to opti.optimize? If I make objective static I can use

opti.optimize(Foo::objective,NULL);

but I do not want to have a static member. Is it possible to create an object of type Foo and give it to opti.optimize?

View 1 Replies View Related

C++ :: Can't Initialize Static Data Member In The Class Definition

Apr 17, 2013

"You cannot initialize the static data member in the class definition — that’s simply a blueprint for an object and initializing values for members are not allowed. You don’t want to initialize it in a constructor, because you want to increment it every time the constructor is called so the count of the number of objects created is accumulated."

Why don't you want to initialize it in a constructor?

Edit: Because every time it is called it will set it back to 0 or whatever the initializing value.

View 2 Replies View Related

C++ :: How To Define Static Member Data In Abstract Class

Jul 11, 2012

For example, in a header file A.h, I define an abstract class,

Code:

// A.h
class A {
public:
virtual void foo() = 0;
private:
static int _x;
};

How'd I initialize static member data _x?Normally, we initialize a static member data in a cpp file. However, there is not cpp file for A.h. If I intialize _x in header file, there will be linker errors like mulitple defined symbols. What is appropriate way to do that?

View 4 Replies View Related

C++ :: How To Create A Base Class Which Defines Static Data Member

Sep 9, 2013

Basically I want to create a base class which defines a static data member so that its automatically redeclared as the same static data member in the derived class.

class A{
protected:
static derivable int val;
// A::val
}

class B : public A{
// static derivable int val is already here
// A::val AND B::val
}

This seems impossible to me but I'm wondering if perhaps there's a way to add modifiers to the compiler to do this (or preferably something MUCH simpler)...

View 11 Replies View Related

C++ :: How To Access Class Member And Methods From Static Method (signal Handler)

Dec 4, 2014

I am writing my program on C++ language. I have one promblem. I need to set signal handler for my process. As the signal is related with the process on system level I have faced the problem.

My program consists several classes. They are connected together. But it doesn't metter in this case. The problem is that I need to access to member and methods of the class from my signal handler. For instance , I have class named Foo at it has some members and methods.

So from my handler I need to call its function and change members.

I understand that compiler should know that this class instances will exist during all program execution.

I have tried to set static member class Foo instance in another class , but this didn't solve the problem.

What is correct approach to do this. How to correctly implement signal handling in such case.

Here is example of my code

Code:
class MyContainer{
private:
std::vector<Foo> container;
public:
int removeFromContainer(Foo* aFoo) {

[Code] .....

View 4 Replies View Related

C++ :: Accessing Non-static Members Inside Static Member Functions

Sep 11, 2013

What are the workarounds for accessing the non-static member variables of some class(Say A) inside static member functions of another class(Say B)? I am coding in c++. Class A is derived with public properties of class B. Any pointers?

View 7 Replies View Related

Visual C++ :: CString Class In Non-MFC Static Library

Sep 13, 2013

I have a non-MFC static library which I share between a number of different projects, some non-MFC and some MFC. Currently the static library uses a typedef of std::wstring and std::string for UNICODE and non-UNICODE builds.

After discovering it's possible to use CString in non-MFC applications, by including atlstr.h header, I decided I'd rather that than using stl strings and having to keep converting between the different types. However, I seem to be struggling with linker errors when linking the library with a MFC application.

Can I create a non-MFC static library using CString from atlstr.h and link it with a MFC application?

View 11 Replies View Related

Visual C++ :: Floating CDialogbar On Multiple Screen Environment

Jul 11, 2014

I have an problem with a CDialogbar if my app runs on a system with two screens (side by side). I can not resize it while the CDialogbar is in floating state on the second screen.

I figured out that the problem is the mfc-function CDockContext::Stretch().

It limits the CDialogbar to the primary screen (using ::GetDesktopWindow() to verify the position).

What can I do?

View 2 Replies View Related

Visual C++ :: Dragging And Dropping Functionality In Windows 8 Environment

Jul 14, 2014

i have developed an application in which i have icons in left side pane of application which can be dragged and dropped in client screen. Application is working fine with all the resolution except 1920x1080.

when setting the resolution to 1920x1080 while dragging icons to client area, icon is not attach to mouse pointer. instead there is a gap between the mouse pointer and the icon. i wrote code to identify the screen resolution but it does not seem to recognize 1920x1080 resolution. below code is giving incorrect resolution for 1920x1080 setting.

RECT actualDesktop;
GetClientRect(GetDesktopWindow(),&actualDesktop);

value of 'actualDesktop' variable is {top=0 bottom=783 left=0 right=1536} which is incorrect. according to current resolution size value should be {top=0 bottom=1080 left=0 right=1920}. Due to this, all the icons while dragging are adjusting according to incorrect resolution setting.

how to identify the issue and if there is any limitation with respect to screen resolution in VC++ 6.0 with windows 8 environment.

I am getting same issue when compiling in VS2012 in windows 8. Code does not seem to recognize 1920x1080 resolution setting and downgrading my application look and feel by setting it to lower resolution.

View 1 Replies View Related

C++ :: Do Static Functions Have Access To Non Static Data Members Of A Class

Apr 17, 2013

From my book:

"A static function might have this prototype:

static void Afunction(int n);

A static function can be called in relation to a particular object by a statement such as the following:

aBox.Afunction(10);

The function has no access to the non-static members of aBox. The same function could also be called without reference to an object. In this case, the statement would be:

CBox::Afunction(10);

where CBox is the class name. Using the class name and the scope resolution operator tells the compiler to which class Afunction() belongs."

Why exactly cant Afunction access non-static members?

View 7 Replies View Related

Visual C++ :: Triangle Class With Member Variables

Apr 8, 2015

C++
Create a Triangle class that has the following member variables:
side1 - a double
side2 - a double
side 3 - a double perimeter area

The class should have the following member functions:

- default constructor that sets the value of all 3 sides of a triangle to 0.0

- a constructor with arguments that accepts values for the three sides of a triangle (member variables) as arguments

- setDimensions - a function that allows the value of the three sides to be entered by the user through the keyboard

- testSides - a function that determines if the 3 values entered can actually be the sides of a triangle. If they do not create a triangle, print the values entered and an appropriate message
--The sum of any two side lengths of a triangle must always be greater than the length of the third side: so side 1 + side 2 > side 3 and side 1 + side 3 > side 2 and side 2 + side 3 > side 1 ( all three must be true for the 3 values to make a triangle)

- getSide1 - a function that returns the value of side 1, getSide2 - a function that returns the value of side 2, getSide3 - a function that returns the value of side 3

- getArea - a function that returns the area of a triangle: The formula for the area of a triangle (when the height is not known) is: A = sqrt (p(p-side1)(p-side2)(p-side3)) where p = (side1+side2+side3)/2

- getPerimeter - a function that returns the perimeter of a triangle: Perimeter = side1 + side2+ Side 3

- A displayTriangleInfo function that displays side1, side2, side3, area, and perimeter for a triangle object.

After testing your code for a single object, create an array of 5 triangles. Use a for loop and the setDimensions function to allow the user to set the values for the 3 sides of a triangle, test the vales entered to determine if the 3 create a triangle. If they do create a triangle than use the getArea and getPerimeter functions to calculate the Area and Perimeter for the triangle and use the displayTriangleInfo function to display all of the data for that triangle. If the three values do not create a triangle then print the 3 numbers entered and an appropriate message. In either case the loop should then move on and get the data for the next triangle from the user.

View 7 Replies View Related

Visual C++ :: Exporting Class Or Member Function?

Oct 2, 2012

Usually we use the following statements to export a class or member functions,

Code:
#ifdef DLLMICRO
#define DLLIO __declspec(dllexport)
#else
#define DLLIO __declspec(dllimport)
#endif

I understand that the files using the exported class or function need to call this class or function with dllimport and the file containing the exported class or function needs to call this class or function with dllexport. But I tried to use __declspec(dllexport) only instead of the statements above. It still works. Is there anything I am missing?Why'd we have to switch between dllexport and dllimport?

View 12 Replies View Related

C# :: Static Method Inside Non-static Class

Aug 22, 2014

Have following code:

class Program
{
static void Main(string[] args)
{

[Code]....

My question according to what i just wrote:

1. Is that mean that Do() is only available for use by Dog itself because Dog is 'oryginal' Dog, and if i create new dogs - instances of oryginal Dog (dog1, dog2 ...) they cant access because Do is only available fo 'oryginal' one? Is that correct thinking?

2. If i would want to have something common (e.g value) for all dogs is that good way to create static field/method for Dog instead of non-static once then all instances of Dog would access Dog static member to get/change it? Just stupid example: static method GetAmountOfLegs() which return 4 Then all instances can take/call that value from Dog. Is that correct thinking?

View 2 Replies View Related

C++ :: Initializing Static Member Just Once Throughout

Jul 24, 2014

Here I'm trying to initialize PersonFactory::ethnicSurnames just once for the entire run of the program:

#include <iostream>
#include <string>
#include <vector>
#include <array>

enum Country {India, China, France, NumCountries}; // plus many other countries
struct School {}; struct Mall {}; struct HockeyArena {};

[Code] ....

Output:

PersonFactory::initializeEthnicNames() called
Carrying out the initialization...
PersonFactory::initializeEthnicNames() called
PersonFactory::initializeEthnicNames() called
PersonFactory::initializeEthnicNames() called
PersonFactory::initializeEthnicNames() called
numberOfTimesInitialized = 1

As you can see, even though five PersonFactory objects were constructed, the ethnicNames initialization only occurred once, as desired. However, there are some issues with my method. First of all, the use of the comma operator is ugly in my opinion. But fashion statements aside, PersonFactory::initializeEthnicNames() is still called multiple times, which is not good, even though it correctly avoids reinitializing ethnicNames after the first call. Also, I now forever get the annoying compiler warnings that the bool namesInitialized is never used, which is true, thus wasting a small bit of memory. And finally, I cannot declare ethnicNames const now, and it is supposed to be const. Any better way to accomplish what I'm trying to do?

By the way, the reason why I don't initialize ethnic names outside the class as is normally done for static data members (and that would indeed allow me to declare it const) is because it would get messed up if I later change the order of the elements in enum Country. Hence actual lines of initializations I think are needed. And I do want ethnicSurnames inside PersonFactory, because I feel it really does belong there. Also, PersonFactory is not to be Singleton, because it has data members that depend on some parameters in its constructor (e.g. geographic location).

View 5 Replies View Related

C++ :: Static Constant Member Initialization

Jun 5, 2013

I am having a problem concerning a static const member variable I want to use to set a certain property of my class during development time. The question actually concerns proper implementation as I do have a solution that "works" at least. The variable should denote the size of a member array which I don't want to allocate on the heap due to serious performance issues. So here is my code:

//MyClass.h
class MyClass{
public:
static const int MyArraySize = 256;
private:
int MyArray[MyArraySize];
};

This works but it's not nice for two reasons:

1) It doesn't separate interface from implementation. I would prefer to define the variable in the corresponding .cpp file but it doesn't work:

//MyClass.h
class MyClass{
public:
static const int MyArraySize;

[Code] .....

If I delete the line int MyArray[MyArraySize]; the above code works but when I use it to define the size of the array I get a "constant expression expected" error for the line int MyArray[MyArraySize]; which makes sense as the compiler does not know the value of MyArraySize when he reaches int MyArray[MyArraySize]; and therefore can not allocate the memory. Of course I can move MyArray to the heap like that:

//MyClass.h
class MyClass{
public:
static const int MyArraySize;
static const int MyValue;

[Code] .....

But as I mentioned before this causes a remarkable loss of performance.

Something like the following does not work:

//MyClass.h
class MyClass{
public:
static const int MyArraySize = (int) pow(2, 8);
private:
int MyArray[MyArraySize];
};

This gives a "constant expression expected" error for the line static const int MyArraySize = (int) pow(2, 8);

Interestingly the following code works:

//MyClass.h
class MyClass{
public:
static const int MyValue;
};

//MyClass.cpp
#include "MyClass.h"
const int MyClass::MyValue = (int) pow(2, 8);

So if I use pow outside of the class definition I get no errors. Is there any solution to those problems? So what I want is:
1) Don't allocate the array on the heap
2) Separate interface from implementation
3) Being able to use functions like pow to define MyArraySize
4) Not use global variables

View 19 Replies View Related

C++ :: Invalid Use Of Non-static Data Member?

May 25, 2014

I am getting this error invalid use of non static data member.my code looks something like this: i have a main.cpp and 2 class files with respective .h files, say one class file is named human (so i have human.cpp and human.h) and stats (so i have stats.cpp and stats.h) in my stats.h file, i have a double array: double HumanF[10][12] with everything filled in.then in my human.h file i just have a bunch of integers. human.cpp has formulas in it that use numbers from the double array i mentioned. for example

Human::Human() {
constant (this is a double i made in human.h) = (1+Stats::HumanF[0][0]);
i (another double) = pow(constant, ylvl);
(ylvl is also an int I made in my header file)
yhp = i*137;
}

View 11 Replies View Related

C++ :: How To Use Functor As Static Constexpr Member

Jan 19, 2014

How does one use a functor as a static constexpr member? I had this basic functor for a class:

struct functor{
short operator()(char c)const{return c-'0';}
};

And in the class, I use it as a static constexpr member:
class Foo{
public:
//...
private:
static constexpr functor k_funky = functor();
};

During the linking stage, I kept getting "undefined reference to 'Foo::k_funky'". So then I tried declaring the functor's constructor and operator function constexpr:

struct functor{
constexpr short operator()(char c){return c-'0';}
constexpr functor() = default; //This counts, doesn't it?
};

But I received the same error.

View 4 Replies View Related

C++ :: Static Variable In Member Function

Aug 27, 2014

I need to keep a static variable in a member function of a class that I have many objects of. I've had some trouble with it, and when I read up I found that such variables are static across all instances. Is there any way around this?

View 3 Replies View Related

C++ ::  Static Member In Struct Array?

Jun 25, 2013

Here's the definition of my struct:

struct Speaker {
static int numElem;
string name;
int number; // Phone
string topic;
float fee;
};

// IN main() FUNCTION
Speaker s[10];

The goal is for numElem to keep track of how many of the 10 elements are in use. However, I'm not sure the proper way to access the element, if it's even possible.

View 7 Replies View Related

C++ :: Getting Static Member / Function Errors

Mar 8, 2014

What am I doing wrong with static members and methods here?

compiler errors:

1>test.obj : error LNK2005: "private: static int Test::count" (?count@Test@@0HA) already defined in main.obj
1>c:usersjamesdocumentsvisual studio 2013Projectsstatic_testReleasestatic_test.exe : fatal error LNK1169: one or more multiply defined symbols found
test.h
#ifndef TEST_H_
#define TEST_H_
class Test {

[code]....

View 4 Replies View Related

C++ :: Static Constant Member Variable

Jun 6, 2013

What is the problem with the following code is? It compiles with Visual C++ 2012 but does not with g++:

//a.h

#ifndef Loaded
#define Loaded
using namespace std;
class MyClass{
public:
static const int MyStaticValue = 200;

[Code] ....

If I try to compile this using the command

g++ a.cpp b.cpp

I get an "undefined reference to 'MyClass::MyStaticValue'" error for the line "A = MyClass::MyStaticValue;" in main(). The strange thing is that if I change the line to "A = (int) MyClass::MyStaticValue;" it works fine and the output is

200
200

as expected.

The code also compiles under g++ if I move the defintion of MyStaticValue from a.h to a.cpp by const int MyClass::MyStaticValue = 200;

View 5 Replies View Related

C++ :: Static Variable Inside A Member Function

Jul 20, 2013

Say you had:

class Foo{
public:
//...
void funky();

[Code] .....

Would each instance of Foo create a new counter variable, or would it remain the same for all of them, i.e. baz.funky() would always use the same counter variable? What if the class was a template?

View 3 Replies View Related

C++ :: Typedef Template With Static Const Member?

Apr 18, 2013

class Tracker {
public:
static const int type;
typedef cv_types::CvType<type>::type_t type_t;
};
const int Tracker::type = 1;

gives me the error:

'I' : invalid template argument for 'cv_types::CvType', expected compile-time constant expression

Shouldn't the static const int be a compile time constant?

How would I specify it, so that it works?

PS.: The code works with #define type 1 at the top of the file and without the static const int.

View 3 Replies View Related







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