You are not logged in.
I have a base class and a bunch of inherited classes.
I am doing something like this in the main.
baseclass* base[2];
base[0]= new _derived1("hello");
base[1]= new _derived2("world");
How do I code my base class or what-ever-else so that I CAN'T do this:
baseclass* base2;
base2 = new baseclass("class");
The base class has 1 constructor with an argument type string. I can't have a default constructor.
So in short. I can create a particular derived class, like a derived1, but can't create a plain base class. (it should throw a compilation error)
Any help would be appreciated.
Thanks.
Last edited by lamdacore (2011-02-06 00:38:43)
Offline
A little code, how you declare/define your classes and member functions--as well as the actual compile error would help a lot.
Guessing out in the blue though, I think it might have to do with the base class being somewhat virtual. I haven't programmed much C++ lately so I might mix stuff up with java, but anyway, more code is needed.
I need a sorted list of all random numbers, so that I can retrieve a suitable one later with a binary search instead of having to iterate through the generation process every time.
Offline
Oh I want a compile error when I do this:
baseclass* base2;
base2 = new baseclass("class");
Example code. So I don't want the base class to be able to make a Base type object. As soon as I do that it SHOULD throw a compilation error.
class Base{
public:
Base(string st):m_st(st)
{}
virtual ~Base(){
}
virtual string ann() const{
return "";
}
virtual string bnn() const{
return m_st;
}
virtual string cnn(string msg) const{
string k="hey ";
k.append(msg);
return k;
}
private:
string m_st;
}
class derived1:public Base{
public:
derived1(string st):Base(st){
}
virtual ~derived1(){
cout<<"kill";
}
virtual string ann() const{
return "hullo";
}
virtual string bnn() const{
return Base::bnn();
}
virtual string cnn(string msg) const{
string k="hey ";
k.append(msg);
return k;
}
private:
string m_st;
}
Last edited by lamdacore (2011-02-06 23:38:23)
Offline
I think you're trying to have your cake and eat it too. You want there to be a compile-time error if you try to construct a Base, but then you want to construct a Base in derived1's constructor. Going about it this way, I don't think there is a way.
Offline
Make the base class an abstract class:
In this case the only reason why the base class should not be able to be used is ann() function:
Define it like this:
...
virtual string ann() const = 0;
...
Edit:
Btw., the second class should not override bnn and then just use the base classes method.
Last edited by draugdel (2011-02-05 12:12:47)
Offline
I know. Exactly why I was wondering how to do this... I didn't make the spec.
I am assuming I have to explicitly have a flag or something that handles the two different cases.
Offline
Thanks draugdel.
Abstract base class or pure virtual functions was exactly what I was looking for.
Make the base class an abstract class:
In this case the only reason why the base class should not be able to be used is ann() function:
Define it like this:
... virtual string ann() const = 0; ...
Edit:
Btw., the second class should not override bnn and then just use the base classes method.
Offline
Yeah, this is one solution of many...
how about this one?
#include <iostream>
class Base {
protected:
Base() { // Constructor must be protected to be accessable in the derived class.
}
public:
virtual ~Base() { }
virtual void doThis() {
std::cout << "Base is doing this..." << std::endl;
}
};
class Derived : public Base {
public:
virtual ~Derived() { }
virtual void doThis() {
std::cout << "Derived is doing this..." << std::endl;
}
};
int main() {
Base *derived = new Derived;
derived->doThis();
Base *base = new Base; // ERROR: Base constructor is protected
base->doThis();
}
edit: Forgot the virtual destructors...
Of course you could also make the Base::~Base destructor pure virtual.
Last edited by xephinx (2011-02-07 08:51:18)
A simple, lightweight distribution - Why the hell 'simple' ? It doesn't sound geekish -.-"
Offline
yup that works too. Thanks.
Are there advantages of using one method over the other? (or is there a convention of some sort?)
Offline
Are there advantages of using one method over the other? (or is there a convention of some sort?)
pardon. I don't understand your question. Do you mean the virtual functions ?
A simple, lightweight distribution - Why the hell 'simple' ? It doesn't sound geekish -.-"
Offline
Yes.
The advantages/differences between the method you showed, versus the method of explicitly declaring a pure virtual function.
Both the ways have the same end effect in my case. However, I am curious is there any other difference between them? Or, is it just a convention thing?
Offline
Always do this using abstract base classes (ones with pure virtual functions), because that's what abstract classes are for. Using protected constructors isn't quite equivalent to abstract base classes either, because the class itself could still construct an instance of itself (for instance, by a static member that returns "new baseclass();").
Offline