Class Question

Hello Community!

First of all sorry for this nondescript title.
My question is about the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   #ifndef TEST_H_
   #define TEST_H_

   #include<string>
   #include<functional>

   class Test : private std::string, private std::unary_function<int, bool>
   {
      public:
         ...
      private:
         ...
   };
   #endif TEST_H_   


When using this, what exactly can one do with it? Has this to do with inheritance of some sort? Meaning that whatever is following the : is then made available to the class itself (because the access specifier is private), or - if public, anywhere this header file is included? If so, how would this be used?
Last edited on
This syntax is to do with inheritance. Everything to the right of that, the class can inherit behaviour from. The access modifier works in exactly the same way as they do for variables and functions.
Private: Can only be accessed by functions and operators in this class.
Protected: The above, and can also be accessed by any functions and operators in a class that inherits from this class.
Public: The above, and can also be accessed from anywhere that includes this header, or from an instance if the function is not static.

C++ Has some very interesting behaviour with multiple inheritance when their's a common ancestor though which you'll need to look into in order to prevent any unexpected behaviour when utilising it. If all the classes you're inheriting from do not share a common ancestor (or you're only inheriting from one class) then it's quite strait forward to work with.
I thought std::unary_function had been deprecated? maybe removed?

Apropos of nothing, if you inherit from two classes both of which have a variable of the same name what (if anything) happens?
Class has two members with same name?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

struct Foo {
  int x = 7;
};
struct Bar : public Foo {
  int x = 42;
};

int main()
{
  Bar bar;
  std::cout << bar.x << ' ' << bar.Foo::x;
}



Further viewpoint:
https://stackoverflow.com/questions/39968498/what-is-the-is-implemented-in-terms-of-relationship-and-when-should-i-use-it
I was actually wondering about inheriting from two, not one class.

Seems I can't do it anyway. Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;


class Happy
{
public:
   string mood = "On cloud nine";
};


class Sad
{
public:
   string mood = "Down in the dumps";
};


class Today : public Happy, public Sad
{
};


int main()
{
   Today Tuesday;
   cout << Tuesday.mood;
}


which elicits
class.cpp: In function 'int main()':
class.cpp:27:20: error: request for member 'mood' is ambiguous
    cout << Tuesday.mood;
                    ^~~~
class.cpp:15:18: note: candidates are: std::__cxx11::string Sad::mood
    string mood = "Down in the dumps";
                  ^~~~~~~~~~~~~~~~~~~
class.cpp:8:18: note:                 std::__cxx11::string Happy::mood
    string mood = "On cloud nine";
                  ^~~~~~~~~~~~~~~
Whoops! Sorry, Keskiverto, I should have read your post more carefully.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;


class Happy
{
public:
   string mood = "On cloud nine";
};


class Sad
{
public:
   string mood = "Down in the dumps";
};


class Today : public Happy, public Sad
{
};


int main()
{
   Today Tuesday;
   cout << Tuesday.Happy::mood << '\n';
   cout << Tuesday.Sad::mood << '\n';
}


On cloud nine
Down in the dumps
First of all thank you, TheBeardedQuack, keskiverto, and lastchance (for the umpteenth time since whenever I tried posting this there was a problem ...)

TheBeardedQuack, thank you for the thorough explanation!

keskiverto, your link pointed me in the right direction, so thanks for it!

lastchance, it is deprecated but does still work. It was useful and only used for the testing purposes which was to write function objects.

The only question that remains is whether it is such a good idea to inherit directly from classes such as string etc. But from what I found it has its uses and isn't uncommon to do it.
Topic archived. No new replies allowed.