Page 1 of 1

Constructor privat in fisier header

Posted: 28 Nov 2017, 18:42
by vili_c++
Va rog sa ma ajutati cu un constructor de tip private.
Costructorul este declarat intr-un fisier header.

test4.h
#ifndef TEST4_H_INCLUDED
#define TEST4_H_INCLUDED

class TestClass
{
private:
TestClass();
public:
static TestClass* getTestClass();
};

#endif // TEST4_H_INCLUDED

main.cpp
#include <iostream>
#include "test4.h"
using namespace std;

int main()
{
TestClass *ptr = TestClass::getTestClass();
delete ptr;
return 0;
}

test4.cpp
#include <iostream>
#include "test4.h"
using namespace std;

TestClass::TestClass()
{
cout << "\n Apel constructor!" << endl;
}

TestClass* getTestClass()
{
return new TestClass();
}

Re: Constructor privat in fisier header

Posted: 28 Nov 2017, 19:05
by Viorel
Încearcă o modificare:

Code: Select all

TestClass * TestClass::getTestClass()
{
	return new TestClass();
}
Dacă mai sînt erori, oferă detalii.

Re: Constructor privat in fisier header

Posted: 28 Nov 2017, 20:25
by vili_c++
Da, acum totul este ok.
Multumesc!