inherit string class

Hi
I'm doing an excercise in my c++ book, and I imediately got in trouble.
So I'm supposed to make a class BCheckString that inherits the class string from the standard library.
BCheckString should have a constructor that takes a string by value and passes it along to the string class constructor.
Ok, so I declared and implemented the class like this:
BCheckString.h
1
2
3
4
5
6
7
8
9
10
11
12
#include <string>

using namespace std;

class BCheckString: public string
{
 public: 
  BCheckString(string s);
  ~BCheckString();

 private:
};

BCheckString.cpp
1
2
3
4
5
6
7
8
9
#include "BCheckString.h"

BCheckString::BCheckString(string s):string(s)
{
}

BCheckString::~BCheckString()
{
}


This compiles without errors, so I think it does what it should. But now I'm confused, where is the string entered in the constructor? How do I access it?
Hi hadoque,

your code is correct. The base classes (string in your case) constructor is called first.

1
2
3
BCheckString::BCheckString(string s):string(s)
{
}


Is where your string is entering. Or maybe I just misunderstood your question.

Hope that helps
You are calling the string constructor, string is a base of your class so the 'string entered in the constructor' is the object of your class
( Your constructor should take a string by const reference )
Hm, yeah, i just realised that.
The excersize description says I should have another member function
char operator[](int k)
that throws an exception if k is negative, equal or greater than the length of the string.
So I need to somehow access the string from inside the class. Is the string just "this"?
(BTW, inheriting std::string is a really bad idea)
Topic archived. No new replies allowed.