Ok given a class that is abstract due to a pure virtual function you can not have an instance of that class. However you can have a pointer of that type. Now that I'm cool with. What he is trying to say is that you can not have a reference of that type either. This is what I have a problem with. So I decided to test it out:
base.h:
Code:
#ifndef MYBASE_H_
#define MYBASE_H_
#include <iostream>
class mybase {
int x;
int y;
public:
inline mybase() : x(0), y(0) {}
virtual ~mybase() {}
virtual void sayhi() = 0;
friend std::ostream & operator << (std::ostream &, const mybase &);
};
#endif
base.cpp
Code:
#include <iostream>
#include "base.h"
std::ostream & operator << (std::ostream &out, const mybase &par)
{
return out<<"Base object: "<< par.x << " " << par.y << endl;
}
dir.h
Code:
#ifndef DIR_H_
#define DIR_H_
#include "base.h"
class dir : public mybase
{
int z;
public:
dir () : z(10) {}
inline dir operator + () { return *this; }
virtual void sayhi() { cout<<"Hello"; }
};
#endif
main.cpp
Code:
#include "dir.h"
int main()
{
dir mydir;
cout<<mydir<<endl;
mydir.sayhi();
cout<<mydir<<endl;
}
Note: Yes I know I forgot some namespaces on cout and endl.
Ok now this compiled and ran as I expected with g++ with the options: -Wall -ansi -pedantic -g
Now this seems to me to indicate that you can have a reference to an abstract class.
Anyone care to comment? (Heres looking at you Sang-drax )
Mostly I need to know if there is some flaw in my test code that is allowing me to do something I'm not suppose to do and/or I'm just misinterpting what I see