Reputation: 383
I have a class that consists of a parameterized constructor which I need to call while creating an object. The class also contains a private copy constructor restricting to create an object to it. Now how to call the paramter constructor of this class. I think we can create a pointer reference to the class. But how to call the parameter constructor using the reference?
My Program:
#include<iostream>
#include<string>
using namespace std;
class ABase
{
protected:
ABase(string str) {
cout<<str<<endl;
cout<<"ABase Constructor"<<endl;
}
~ABase() {
cout<<"ABASE Destructor"<<endl;
}
private:
ABase( const ABase& );
const ABase& operator=( const ABase& );
};
int main( void )
{
ABase *ab;//---------How to call the parameter constructor using this??
return 0;
}
Upvotes: 0
Views: 93
Reputation: 589
You can't do this. Because your ctor is protected
. Please see (not related with your state, but only to learn more): Why is protected constructor raising an error this this code?
Upvotes: 1
Reputation: 234715
The syntax you need is ABase *ab = new ABase(foo);
where foo
is either a std::string
instance or something that std::string
can take on construction, such as a const char[]
literal, e.g. "Hello"
.
Don't forget to call delete
to release the memory.
(Alternatively you could write ABase ab(foo)
if you don't need a pointer type.)
Upvotes: 1