I want have two or more classes inside a shared libary in Qt C++.
What is the way to make a shared library(dll) holding two or more classes?
Do they need to be nested inside a parent class or should I use namespaces?
What is the easiest or elegant method that you would direct me or give an example?
All classes are independent; and I only want to access proper class from the application. There may be many classes; so nested classes inside a single parent class would not be readable really. I thnik there is no partial class support in C++ . Also I am not sure if nested class and the namespaces are the only way to do that(?)
class A
{
public:
A();
void TestA();
};
class B
{
public:
B();
void TestB();
};
Application:
MyLib::A a;
MyLib::B b;
Here I have no idea how to implement MyLib.
For those who are interested with the resolution is simply exporting the class
Q_DECL_EXPORT class A
{
public:
A();
void TestA();
};
Related
I have this large class that I want to separate into different classes. The reason why it was large because the class had many private variables and functions that are needed to run the program. I was tired of scrolling down the 1000+ lines of code trying to add or edit code. I am wondering if it is possible for the classes to interact with one base class that includes all the private/protected variables it needed to operate, or simply have them as global variables.
I am aware of inheritance as I tried having the separate classes be derived from the base class, by doing something similar to this:
#include <iostream>
class Base {
public:
void sayPrivateVar() {
std::cout << privateVar;
}
protected:
std::string privateVar = "I am a protected variable!";
};
class Derived : public Base {
public:
void manip() {
base->privateVar = "That was updated from a derived class";
}
private:
Base* base;
};
int main() {
Base base;
Derived derived;
derived.manip();
base.sayPrivateVar();
return 0;
}
EDIT: Without creating another object inside a class.
it depends on your class and what you have in it. It is often better not have inheritance because derived classes may then get member variables that they don't need. There are a few other ways you can do. One way would be to group you private variables in different classes and then have member variables in the first class.
E.g.
class X {
int x;
int y;
int angle;
...
};
becomes
class XYVector;
class X {
XYVector v;
};
class XYVector
{
int x;
int y;
int angle;
};
You can continue in this direction and instead of making them concrete class like XYVector above have them as interfaces : to have it more elaborate and flexible check out https://en.wikipedia.org/wiki/Composition_over_inheritance
At any rate: avoid having globally declared variables.
This is a good question and the answer is absolutely. Inheritance is actually a very good solution in this particular context since that is how object code shares it's scope with other classes. One important thing to note here is that how you call your derived class is important because the inherited scope is set along with the derived class (i.e. declaring it public base would inherit the public and protected methods as opposed to declaring it private which would give the derived class even more access!)
I want to put all of a classes functions into a "subdomain" (what is the right term for this?), such as the functions part of myClass.functions.function1();
So how should one rewrite this for that purpose?
class MYCLASS {
private:
int data;
public:
int function1();
int function2();
int function3();
};
So that the class could then be used:
MYCLASS myClass;
myClass.functions.function1();
rather than using just myClass.function1();.
You can make these free functions in some descriptive namespace, or you can emplace them inside a nested class. However, both of these imply access to members only through the original class's interface, meaning you will still need some public functions inside the class itself.
Otherwise, it's already clear that these are functions, so I would simply not do this. If you want to because you have loads and loads of functions, then you have too many functions, and your class almost certainly does too many things.
Depending our your design (which needs to be object-oriented but not necessarily) you can define a new namespace or an struct or a class which is called nested classes!
for example:
class A {
public:
int a;
class B {
public:
void do_something();
} bObject;
};
I am new at building distributable libraries written in C++ and I am getting a bit lost.
I have created a .cpp file with wrappers for all functions I want the library to offer users, and I have written 2 .h files, one public and one private. Below is a dummy example of my header files:
public.h:
class myclass
{
public:
public_function();
private:
}
private.h:
class myclass
{
public:
public_function();
private:
anotherClass instanceofClass;
}
Note that the implementation of public_function() uses the "instanceofClass" in the code.
I have been able to compile with no problem the code using the private class and to compile and link the library with external programs using the public header and the compiled library. When executing that code, though, I am getting segmentation faults that I suspect have to do with lack of proper initialization of "instanceofClass".
Am I doing the right thing? Am I forced to instantiate "instanceofClass" inside the implementation of public_function() for it to be initialized properly, or is there anything else I should do instead?
Thanks a lot.
You can't declare the same class 'myclass' in two different ways. There has to be a single class definition. If you want to hide the implementation's API you want to use the 'Pimpl' idiom. So your public class has a single pointer to a private class. For example:
public.h
class myclass_private;
class myclass {
private:
myclass_private* pimpl;
public:
myclass();
void public_function();
};
public.cpp
myclass::myclass() {
pimpl = new myclass_private;
}
void myclass::public_function() {
pimpl->private_function();
}
private.h
class myclass_private {
public:
void private_function();
};
The myclass defined in public.h has no members, and is therefore sized 1 byte. The myclass defined in private.h encapsulates anotherClass, and is therefore whatever size anotherClass is. This inconsistency is the root of your problem.
What you ought to do is have only one header, and use a pointer (which doesn't require a class definition) to enable hiding the implementation of anotherClass. I'll repeat Joachim's link to the pimpl idiom for elaboration.
The definition of a class shall not changr between different translation units. This is one of the aspects of the One Definition Rule. What you might want to donis to define the publicly visible class to have a pointer to a private implementation: the Pimpl Idiom:
class Public {
public:
...
private:
struct Impl;
Impl* impl_;
};
The struct Impl would only be defined in the implementation file.
Your class lacks a proper constructor, which means that the compiler will provide a default one based on the content of the class definition. If that definition isn't consistent accross all the code, it won't get initialized the same way everywhere, and some data may be missing.
If you want to hide the implementation details of instanceofClass, just do a forward declaration in the header (the private header you're providing is correct, you can use it as your public one), and provide an implementation somewhere in your code.
I have a C-header file with 100s of data structs. I want to make a superclass that each data struct inherits from. How to do that? Should I parse the header file? Reflection?
Because each C data struct is a public class, I could just make a superclass and... then what? I should not go in manually and explicitly make every struct inherit from the superclass?
If you have a bunch of structs:
The obvious answer is to make clever use of search and replace to make them inherit from the new base class. It wouldn't be able to access the members of the structs, but you could store pointers to them all in containers
struct base {
int dostuff() const {std::cout << "thing";}
};
struct thing1 : public base {
int size;
};
//otherstructs inheriting from base
int main() {
std::vector<std::unique_ptr<base>> objects;
objects.push_back(std::unique_ptr<base>(new thing1()));
objects[1].dostuff();
}
If making the base class thing is too hard, you might try adding functions via a wrapper class instead. You won't be able to store pointers to these in a container, but they can access members, as long as the structs have similar members.
struct thing1 {
int size;
};
//other unchanged structs
template<class mystruct>
struct wrapper : mystruct {
int getsize() {return mystruct.size;}
};
int main() {
wrapper<thing1> mything1;
std::cout << mything1.size << mything1.getsize();
}
Or, if you're just adding methods, make them separate functions:
struct thing1 {
int size;
};
//other unchanged structs
template<class T>
int getsize(const T& obj) {return obj.size;}
int main() {
thing1 mything1;
std::cout << mything1.size << getsize(mything1);
}
For your particular use case, I would leave the C code as is, and just create whatever solution you need in C++ leveraging the C code. You can for example, create a parallel hierarchy with a base that defines the interface that includes serialize() and deserialize() and each derived type storing one of the C structs and implementing the serialization of that particular type.
You can also tackle this completely externally, by providing templated serialize/deserialize functions that are defined for each one of the C structs... Inheritance is one of the most often abused features of C++.
The best solution will depend on how you intend on using the code, but I would avoid rewriting the C code (i.e. scripting an editor to rewrite the C header into a C++ solution) as that will effectively branch out of the C library and you will need to maintain two separate branches if you ever need to extend it C side.
I have never heard of structs using inheritance - it might be standard practice somewhere but all the examples of inheritance have been using a class - will it create a hardship to change the struct to a class? This could be confusing as I tend to think "C" when I see a struct.
Also are you sure that with inheritance, you still need 100's of structs or classes? If they really don't have duplicate data, that is OK, but if there are duplicated fields, it might be a good idea to have them inherit from each other to remove the duplicate fields.
There is no reflection in C/C++, and there is no one base class for other classes in C++, so this isn't an easy task. You can manually make every class you have inherit from the superclass as you suggested, or you can create a base class and then create a new structure for each data structs, which inherits it's concrete data struct and also the base class.
For example http://liveworkspace.org/code/7e06e0374ef41bc4aeeafd55ae143527 or http://liveworkspace.org/code/7e06e0374ef41bc4aeeafd55ae143527.
I think manually making each struct inherit from the base class in your C-header file is more efficient.
I have a class that only really ever needed by classes in a certain class hierarchy. I wanted to know if it is possible to nest the class in the highest class's protected section and have all the other classes automatically inherit it?
"Inherit" is the wrong word to use since it has a very specific definition in C++ which you don't mean, but yes you can do that. This is legal:
class A {
protected:
class Nested { };
};
class B : public A {
private:
Nested n;
};
And code that is not in A or something that derives from A cannot access or instantiate A::Nested.