abstract via constructor

i want to make abstract class, but I usually hears that we can't do that without pure virtual function , but i'm trying to do that via constructor but without virtual

like this

[code]#include <iostream>
using namespace std;
class base{
int x,y;
public:
base(int q,int w)=0;

};
class deriverd :public base{
public:
deriverd();
};



base::base(int q, int w){q=w=0;}

deriverd::deriverd():base(0,0){;}

but i wonderful , because i don't ensure that it's perfect abstract class
can help me that if my code have any probability to perfect-less abstract class, or my code have to be error prone ??
Last edited on
The pure-specifier that is "= 0" may be used only in the declaration of a virtual function. A constructor can not be virtual.
But you can declare a destructor as a pure virtual function
It's not possible to have pure virtual constructors. If you just want to make it impossible to create instances of base you can make the constructor protected.
1
2
3
4
5
class base{
	int x,y;
protected:
	 base(int q,int w);
};

¿what would be a virtual constructor? ¿how would you intend to use it?
1
2
3
4
base *ptr;
//...
ptr = new derived;
base *q = ptr->create();
It seems that your princess is in another castle.
I want to make abstract class just , i not intend to do any virtual pure function , i asked for if i want to make that abstract class just , is my code correct?
An abstract class has at least one pure virtual function, which means it has to be inherited to be used.

If you just want inheritance then make sure your class's methods are virtual but don't do the "=0" thing.

No, your code is not correct, you can't have virtual constructors. Also, your constructor is not even virtual, just pure, which is also illegal.

1
2
3
4
5
6
7
8
9
10
11
class Base
{
public:
    virtual FuncWithPolymorphism(); //Base function
}

class Derived: public Base
{
public:
   virtual FuncWithPolymorphism(); //Redefinition
}
@OP: no, you can't do it. By definition.
the abstract concept for me , that the class you can't instantiate object of it.


so if i won't to gain an abstract class and derived class that inherit it .
such that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
class base{
	int x,y;
public:
	 base(int q,int w)=0;

};
class deriverd :public base{
public:
	deriverd();
	};



	base::base(int q, int w){q=w=0;}
	
	deriverd::deriverd():base(0,0){;}
void main(){
	base object(0,0);
}

the error that it happened
: error C2259: 'base' : cannot instantiate abstract class 

according my abstract definition my class is correct.
then i can make abstract class base(int q,int w)=0;
???????
I think your view of what an abstract class is doesn't match the C++ definition.

It might be better to step back and describe what you want to do, and let someone show you how to achieve that effect in C++.
Topic archived. No new replies allowed.