Constructor privat in fisier header

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
vili_c++
Junior
Junior
Posts: 17
Joined: 11 Feb 2014, 23:39
Judet: Dolj

Constructor privat in fisier header

Post by vili_c++ » 28 Nov 2017, 18:42

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();
}



Viorel
Microsoft MVP
Microsoft MVP
Posts: 287
Joined: 13 Jul 2007, 12:26

Re: Constructor privat in fisier header

Post by Viorel » 28 Nov 2017, 19:05

Încearcă o modificare:

Code: Select all

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

vili_c++
Junior
Junior
Posts: 17
Joined: 11 Feb 2014, 23:39
Judet: Dolj

Re: Constructor privat in fisier header

Post by vili_c++ » 28 Nov 2017, 20:25

Da, acum totul este ok.
Multumesc!

Post Reply