#pragma once
#include "ClassB.h"
class ClassA
{
public:
ClassA(void);
~ClassA(void);
private:
ClassB myClassB;
};
File: ClassA.cpp
#include "StdAfx.h"
#include "ClassA.h"
ClassA::ClassA(void)
{
myClassB = ClassB(12);
}
ClassA::~ClassA(void)
{
}
What I see happening when I test this, is ClassA.h line 10 is creating an instance of ClassB with no parameters. Then ClassA.cpp line 6 is creating another instance of ClassB with parameter 12, the ClassB constructor is called which handles the parameter 12, then when the ClassA constructor exists, the destructor for ClassB is called, indicating the class I just created is being deleted.#pragma once
#include "ClassB.h"
#include "ClassC.h"
class ClassA
{
public:
ClassA(void);
~ClassA(void);
private:
ClassB myClassB;
ClassC myClassC;
};
File ClassA.cpp
#include "StdAfx.h"
#include "ClassA.h"
#include "ClassC.h"
ClassA::ClassA(void)
{
myClassB = ClassB();
myClassC = ClassC(myClassB);
//further lines...
}
ClassA::~ClassA(void)
{
}
This is a stripped down version of my actual project. Something isn't going right in the initialization of my actual project. When it executes line 8 of ClassA.cpp it skips all further lines like there's some internal error. Anything wrong with this code?
ghMutex = CreateMutex(
NULL, // default security attributes
FALSE, // initially not owned
NULL); // unnamed mutex
Then in the destructor I release that mutex:
CloseHandle(ghMutex);
Maybe there's a better way to synchronize access to an integer so I don't have two threads, one writing the other reading the variable at the same time?
ClassA::ClassA(void) : myClassB(), myClassC(myClassB) // 'myClassB' here just serves the purpose to enforce the construction before 'myClassC'
{
//further lines...
}
In this initializer list list it looks like we're storing myClassB in variable myClassC.How would I use InterlockedExchange to protect a float or a double?there is a fast mutex class which uses the InterlockedIncrement and InterlockedDecrement to grant exclusive access to any other resource.
class FastMutex
{
volatile long lock;
public:
FastMutex() : lock(0) {}
void enter()
{
while (InterlockedIncrement(&lock) > 1)
{
InterlockedDecrement(&lock);
Sleep(0);
}
}
void leave() { InterlockedDecrement(&lock); }
};
Anything wrong with this code?
classA::classA() : myclassB(this), myClassC(this) { }
classA::classA() : classB(this) {}
classB::classB(classA * pA) : pmyClassA(pA), myClassC(this) {}
classC::classC(classB * pB) : pmyClassB(pB) {}
myClassB = ClassB();this statement makes no sense in a c++ constructor because the myClassB already was created before it was entering the constructor's. that is the reason why c++ has an initializer list outside of the constructor's body to initialize members and create class members with a non-default constructor. with the statement you made you create an additional temporary classB object and assign it to the already created myclassB object. if classB has a default constructor which correctly initializes all members that copy+assign doesn't make anything worse. but nevertheless it is not needed and error-prone. if myClassB was initialized with an existing member, the assignment would destroy the contents of the member and its origin.