More questions by me...

1. CDerived inherits from CBase. There is a global function that takes a reference to a CBase object:

int Function(CBase& param) { /* ... */ }

Can I execute the function with an object of CDerived as the parameter?
(like this):

1
2
3
int a;
CDerived obj;
a=Function(obj);


2. Let's say there are few classes. Some of them have members that are objects. In other words, class A cannot be declared before the others because it has a member of type (class) B, so class B has to be declared first. But there's also class C and a one of B's members is a C object and so on...My questions is: can a class be declared without specifying its members, so I could use objects of it in other classes? Like this:

1
2
3
4
5
6
7
8
9
10
11
class A;

class B
{
    A a;
}

class A
{
   /* ... */
}



3. Let's say I have a short member function and I want it to be inline. I know it can be done by defining it inside the class declaration, but let's say I prefer to put all the member function definitions in one cpp file. Can I put the function definition outside the class declaration with the inline keyword?
Like this:

1
2
3
4
5
6
class SomeClass
{
    int MemberFunction();
}

inline int SomeClass::MemberFunction() { /* ... */}

It would be nicer if I could define all the member function of a class in one file.

4. Can #include be used in a .h file? (probably a stupid question...)

5. If the answer to question 2 is yes, what happens if class A has a pure virtual function? Does the compiler recognize the problem?



Last edited on
For question 1,2,3 and 5, I would suggest: try it out! That's the best way to learn it (I would answer them if I had known the answers, but I dont. However, it takes only a few minuts to write some short programs and find it out).

The anser to question 4 is: yes, you can. I try to avoid it and put all the #include's in the main-cpp file. When writing a headerfile, I use comments on top of it where I tell myself wich librarys are used inside the headerfile.
1. Yes

2. Yes. Forward declaration it is called. It's a useful technique for reducing the amount of #includes in your header files, and resolving cyclic dependencies.

3. Yes. However, you must put the implementation of template functions in your header file (If I remember correctly).

4. Yes, but best practice is to use forward-declaration then #include in the .cpp file

5. Having pure virtual functions at this point doesn't matter. As long as you #include before you actually use the type you've declared.
With regard to question 2 - (and I see a lot of questions in the forum of this type Am I allowed to do this.... or Can I do this......).

The simple answer to questions like this IMHO is to try it and see what the compiler says.
@guestgulkan: IMO better to ask. That way someone can point out reasons for/against doing that. It's a good way to know what are good and bad practices when writing software. Especially if you can have professionals answer the question.
Thanks a lot for the answers! :)

More questions:

6. I asked about pure virtual functions in question 5 because if class A has a pure virtual function, class B cannot have an object of it. Will the compiler recognize this problem?

7. If class CDerived inherits from class CBase, do the inheritance details need to appear in CDerived's forward declaration (class CDerived : public CBase;) or inheritance doesn't matter (class CDerived;) ?

8. A project has more than one class, and each class has a .h file and a .cpp file. The classes depend on each other. I created a classFwdDec.h file and put there forward declarations of all the classes. In each .h file which belongs to a class, the first line is #include "classFwdDec.h" . Is this the common way for eliminating problems with declarations? (All the .h files have header guards)
Or it be smarter to simply put all the internal .h files (i.e. the ones belonging specifically to this project) in one .h file and include that file in all the cpp files? (like this:)

FirstClass.h :
1
2
3
4
class FirstClass
{
    /* ... */
}


SecondClass.h :
1
2
3
4
class SecondClass
{
    /* ... */
}


ThirdClass.h :
1
2
3
4
class ThirdClass
{
    /* ... */
}


classFwdDec.h :
1
2
3
class FirstClass;
class SecondClass;
class ThirdClass;


classes.h : (this one will be included in all cpp files)
1
2
3
4
5
6
7
8
9
#ifndef CLASSES_H

#define CLASSES_H
#include "classFwdDec.h"
#include "FirstClass.h"
#include "SecondClass.h"
#include "ThirdClass.h"

#endif 







Last edited on
Where are you getting these questions? It is almost as if you are asking the questions with enough understanding that I would expect you to know the answers already!
Last edited on
6. Yes, the compiler will error.
7. Inheritance Doesn't matter
8. Only forward declare what you need in the header file of the object using it. Central declaration objects are a bad idea. They make it very difficult to maintain the code.
Last edited on
7. I don't think class x : public y ; is a valid forward declaration
@seymore15074 : I'm working on my first C++ project, so I have lots of questions. The tutorial doesn't mention many things, you know. So I'm asking. You see, I don't have any experience, but I read the tutorials at cplusplus.com and learncpp.com, so I have the basic knowledge.

@Zaita : Thanks for all the answers! You helped me a lot :)

@guestgulkan : Indeed, it isn't. Class forward declarations do not include the inheritance part, so it would be just class x; . I learned it few minutes ago (from the answer to question 7) . . .
Topic archived. No new replies allowed.