Click here to Skip to main content
16,021,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone!
I need to call a function in this way:
nmsp.Operate( Nmsp::Derived{}.SetName( "New Name") );


So, I can't understand how to realize this.

Thank u for your help in advance.

P.S.
if I use unique_ptr I get an error:
Visual studio compiler: Error C2259 'Nmsp::Base': cannot instantiate abstract class


What I have tried:

C++
#include <iostream>

namespace Nmsp
{
	class Base
	{
	public:
		virtual Base& SetName(const std::string& c) = 0;
		virtual ~Base() = default;
		virtual void Func(std::ostream& out) const = 0;

		double param = 1;
	};

	class Derived : public Base
	{
	public:
		Derived()
		{
			std::cout << "Derived Ctor" << ", name=" << name << std::endl;
		}

		Base& SetName(const std::string& c) override
		{
			name = c;
			param = 2;
			std::cout << "SetName" << ", New name=" << name << std::endl;
			std::cout << "SetName" << ", New param=" << param << std::endl;

			return *this;
		}
		void Func(std::ostream& out) const override
		{
			out << "Derived param = " << param << std::endl;
			out << "Derived caption = " << name << std::endl;
		}
		~Derived()
		{
			std::cout << "Derived Dtor" << ", name=" << name << std::endl;
		}
		std::string name = "Default Name";
	};

	class Holder //: public Base
	{
	public:
		~Holder()
		{
			if (basePtr)
				delete basePtr;
		}
		template <typename T>
		void Operate(T& bPtr)
		{
			basePtr = &bPtr;
			//basePtr = std::make_unique<T>(std::move(bPtr));
		}

		void Go(std::ostream& out)
		{
			basePtr->Func(out);
		}
	private:
		Base* basePtr = nullptr;
		// std::unique_ptr<Base> basePtr;
	};
}

int main()
{
	Nmsp::Holder nmsp;

	Nmsp::Base* pBase = new Nmsp::Derived;
	*pBase = Nmsp::Derived{}.SetName("New Name");
	nmsp.Operate( *pBase );

	std::cout << "before go" << std::endl;

	// Need to call the function Operate() in this way
	//nmsp.Operate( Nmsp::Derived{}.SetName( "New Name") );

	nmsp.Go( std::cout );

	return 0;
}
Posted
Updated 9-Oct-20 9:59am
v3
Comments
[no name] 9-Oct-20 5:50am    
Your question is not clear. What is the actual problem?
bartello 9-Oct-20 16:16pm    
Here is the actual problem: if I use the commented line in function main() instead of three lines (where the function Operate() is called) - the code will work not properly.
[no name] 10-Oct-20 3:56am    
"the code will work not properly."
What does that mean?

1 solution

That is what you have implemented. Maybe you can remove the Nmsp to make live easier or write
C++
using namespace Nmsp;
Your Operate function needs a Derived object reference as input, so an overriden
C++
void Operate(T *bPtr)
{
	// reactivated code (why did you use it?)
	basePtr = std::make_unique<T>(std::move(bPtr));
}
should work.
 
Share this answer
 
Comments
bartello 9-Oct-20 12:03pm    
KarstenK, thank u for answer, but I commented that code, because it does not work!
Visual studio compiler: Error C2259 'Nmsp::Base': cannot instantiate abstract class

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900