C++, object declaration in header files - c++

I have some class, and in it I want to create object of another class ...
Usually I do it in header file, I just put something like:
QString RSSName;
and it works because that class has constructor that has no parameters ...
So here's my problem: how do I do that for some class(let's say ErrorOutput) that has only constructor with 1 or more parameters? I don't want to create pointer to object, I need it to be something like this:
ErrorOutput err("test");
I hope I've described the question correctly, it's little sleepy over here :P
Thanks for help :)

It's a bit hard to tell from your description what exactly you are asking for, but it sounds like "RSSName" is a member variable in your class. If I'm correct about that, initialize it in the constructor's initialization list.
class Foo
{
public:
Foo() : RSSName(someVal) { }
private:
QString RSSName;
}

Related

Name of the object C++

is there a way to let the constructor of the object know its name?
I would like to have string myObject.name created by the constructor based on the actual name of the object. Is it possible?
myClass Example;
//constructor creates an object with this name and i would like to have Example.name=="Example";
Thank you for your help!
There's no guarantee that it actually has a source name of any kind, so there's no sense in such a mechanism. And furthermore, tying your user display to the internal implementation details of your source code is a tremendously bad idea.
You, of course, can assign it whatever name you like that you think has whatever meaning you like.
There is no such inbuilt functionality.
Although you can build one for yourself, following is the one way how you can achieve it.
class myClass {
std::string m_objectName;
public:
myClass(std::string name):m_objectName(name);
std::string name()
{
return m_objectName;
}
};
Now create object like this:
myClass Example("Example");
Not very useful, but with a macro you can achieve something near:
#define CREATE( a, b ) a b(b)
int main()
{
CREATE( myClass, example );
}
assuming of course that myClass has a constructor with a string as argument.
On the bottom line, I agree with the other answer, there really no point doing this.

data hiding in C++

I have some code in C, that uses incomplete structs this way ( simplified example ):
something.h
struct something;
struct something *new_something();
int work_a(struct something *something);
int work_b(struct something *something, const char *str);
void free_something(struct something *something);
somecode.c
int some_function()
{
struct something *something;
int x;
something = new_something();
x = work_a(something);
free_something(something);
return x;
}
I was thinking, I'm basically doing C++ here, why not try write it in C++ .
The question is ( I'm new to C++ ), how do I achieve the same in C++ ? If I try to add declare a member function of an incomplete class, I get
error: incomplete type 'something' named in nested name specifier
from clang. By writing the complete class in the header, this would lose the whole point of data hiding, and changing private vars in the class would force every file including "something.h" to recompile, which I think is not needed here. I don't need the files using "something.h" to know the size of this struct / class, I'm usually fine with having just a pointer. I suspected it should look like this:
class Something;
Something::Something();
Something::~Something();
int Something::work_a(int x);
this way I could write the same thing I did in C, only shorter, and even cleaner. Any C++ coder out there wishing to enlighten this mortal C coder?
Take a look at this article: Hiding Implementation Details in C++. It should get you pointed in the direction you are looking. Note that inheritance is being used to accomplish the goal. Also understand that in C++, a struct is a class with all members having public access (includes functions, constructors, and destructors). At a minimum, the interface has to be declared a class, then inherit from that publicly in the now hidden class implementation inside the cpp file (not another header file).
On the Pimpl design pattern, check out this Stack Overflow article: pimpl idiom vs. bridge design pattern. It should also help.
One way to achieve this is through the Pimpl design pattern where you have a pointer to some private struct/class that only your implementation knows about. Your private functions use the pointer and in theory it can be mostly inlined.
When you allocate memory with new statement the compiler has to know how much data space to allocate. The data size of Something has be seen by the compiler before you can use new to create a Something instance.
Use something like this in Something.h
class Something {
public:
Something();
private:
struct HiddenData;
HiddenData* m_pHidden;
};
Use something like this in Something.cpp
struct Something::HiddenData {
int a;
int b;
};
Something::Something() : m_pHidden(new HiddenData()) {
m_pHidden->a = 1;
}

Define a COM smarter pointer as a member in header file?

I am wondering how to define a COM smart pointer in a header file as a class member? Here is what did:
In .cpp file, I have:
long MyClass:MyFun(long &deviceCount)
{
RESULT h = CoInitialize(NULL);
MyComPtr ptr(__uuidof(MyComClass));
if(deviceCount > 0)
ptr->Connect();
}
But since other functions need to use ptr, I am thinking about changing it to a class member and define it in the header file, something like this:
In .h file:
MyComPtr _ptr;
then in .cpp file, I have:
_ptr(__uuidof(MyComClass));
But the compile did not go through, it says "term does not evaluate to a function taking 1 argument". I am very confused how I can implement this. Any ideas? Thanks.
EDIT: So to use initilizer list, it shoule be something like this?
MyClass:MyClass() : _ptr(new MyCom)
{
_ptr(__uuidof(MyComClass));
}
The initializer list is called at construction time to set variables that would otherwise be const. It's commonly used for const variables, references, etc. I don't actually know COM, but if the smart pointer has similar mechanics to a reference (i.e. once set it cannot be retargeted) then it will have to be initialized at construction time, using an initializer list.
Constructor() : _Ptr(new MyComObject)
{
// Other constructor stuff here
}
The syntax is probably wrong - as I said, I don't know COM - but this might be helpful?
EDIT:
Assuming you have the following class:
class MyClass
{
public:
MyClass(); // constructor
MyComPtr _ptr;
};
Then in your .cpp, define your constructor like this :
MyClass::MyClass() : _ptr(__uuidof(MyComClass)
{
// rest of constructor code
}

Object Construction in C++

I am working on a small project in C++ that requires me to create an object of a custom class I wrote in another one of my classes. The class is called FIRFilterModule, It has a simple blank constructor.
Being of a java background, my impulse is to create it like this:
class SensorInput{
public:
FIRFilterModule firFilter;
...More Class Members...
SensorInput():firFilter(FIRFilterModule()){}
...};
However this compiles with the ever so helpful error message of "Error within this context". I'm a little lost why that doesn't work. Increasing my confusion I changed the code to this:
class SensorInput{
public:
FIRFilterModule firFilter;
...More Class Members...
SensorInput(){}
...};
It works.
Can someone help me understand why this is so?
In this particular case, running of the default constructor for a member field, you don't have to do anything. The constructor is run automatically. So you can just write
class SensorInput{
public:
FIRFilterModule firFilter;
SensorInput() { ... }
};
The member initialization list is only needed when you need to call a constructor which has arguments or initialize POD types. For example say the FIRFilterModule had a constructor which took an int. Then you would use the memeber initialization list
SensorInput() : firFilter(42) { ... }
The code you posted is correct.
Maybe you forgot to include the header where FIRFilterModule is declared.
Otherwise, everything should work.

How can you call a c++ constructor somewhere other than where you define a variable without calling new?

I have a class set up like this:
class Foo {
Foo();
private:
Bar m_bar;
}
That is the class definition in it's own header file, and now in the source file to go with it I have the constructor and I tried doing this:
Foo::Foo() {
m_bar("parameters for the Bar constructor");
}
However this doesn't work and gives me an error. I can make m_bar a pointer and then in Foo's constructor do this:
m_bar = new Bar("parameters here");
However that makes m_bar a pointer and I don't want that.
I'm not the best with C++ classes and pointers, so could something either explain a way for me to have m_bar defined in the Foo class but constructor somewhere else or if it is better to make m_bar a pointer in this situation explain why? While I would rather not make it a pointer(because I don't understand pointers extremely well), if that is the best way to do it then I would rather do it that way, but I'd still like someone to explain why that is the best way to do it(if it is).
Yes, using the initializer list syntax:
Foo::Foo() :
m_bar("parameters for the Bar constructor")
{
}
You need to use initialization lists:
Foo::Foo() :
m_bar("and you ought to check out a good C++ book :)")
{
// Book list: http://tinyurl.com/so-cxxbooks
}
If I'm understanding you right, you can use the constructor initialization list to do so.