Click here to Skip to main content
14,767,933 members
Home » Discussions » C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: C++ class question Pin
Mircea Neacsu8-Dec-20 8:29
mvaMircea Neacsu8-Dec-20 8:29 
QuestionRe: C++ class question Pin
David Crow8-Dec-20 8:06
MemberDavid Crow8-Dec-20 8:06 
AnswerRe: C++ class question Pin
pkfox8-Dec-20 8:16
professionalpkfox8-Dec-20 8:16 
SuggestionRe: C++ class question Pin
David Crow8-Dec-20 8:19
MemberDavid Crow8-Dec-20 8:19 
AnswerRe: C++ class question Pin
Joe Woodbury8-Dec-20 11:00
professionalJoe Woodbury8-Dec-20 11:00 
AnswerRe: C++ class question Pin
CPallini8-Dec-20 22:29
mveCPallini8-Dec-20 22:29 
GeneralRe: C++ class question Pin
pkfox9-Dec-20 4:19
professionalpkfox9-Dec-20 4:19 
GeneralRe: C++ class question Pin
CPallini9-Dec-20 8:28
mveCPallini9-Dec-20 8:28 
Quote:
which is not a problem but I'd like to know how to do it the other way
There isn't 'the other way' (AFAIK).

In C++, using new, you get a pointer. Moreover you need to handle the cleanup of the dynamically allocated memory:
#include <iostream>
using namespace std;


namespace Test {
  class Dummy
  {
    string path;

  public:
    Dummy(string path) : path {path}{}
    string get (){return path;}
    void set(string newpath){ path = newpath; }
  };


  class Program
  {
    Dummy * dummy;

  public:
    static void main()
    {
      Program * p = new Program();
      p->dummy = new Dummy("MyPath");


      cout << p->dummy->get() << "\n";

      // cleanup 
      delete p->dummy;
      delete p;
    }

  };
} // <- Test


int main()
{
  Test::Program::main();
}


You may write the Program destructor to ammeliorate the code, but cleanup is unavoidable.

If you can initialize at once your objects then you could use the stack and the code would be nicer:
#include <iostream>
using namespace std;


namespace Test {
  class Dummy
  {
    string path;

  public:
    Dummy(string path) : path {path}{}
    string get (){return path;}
    void set(string newpath){ path = newpath; }
  };


  class Program
  {
    Dummy dummy;

  public:
    Program( Dummy dummy):dummy{dummy}{}
    static void main()
    {
      Program p { Dummy{ "MyPath" } };
      cout << p.dummy.get() << "\n";

      // no cleanup needed
    }

  };
} // <- Test


int main()
{
  Test::Program::main();
}



Finally smart pointers allows you to make the two steps initiazlization without the hassle of manual cleanup, e.g.

#include <iostream>
#include <memory>
using namespace std;


namespace Test {
  class Dummy
  {
    string path;

  public:
    Dummy(string path) : path {path}{}
    string get (){return path;}
    void set(string newpath){ path = newpath; }
  };


  class Program
  {
    unique_ptr<Dummy> dummy;

  public:
    static void main()
    {
      Program p {};
      p.dummy = make_unique<Dummy>("MyPath");

      cout << p.dummy->get() << "\n";

      // no cleanup needed
    }

  };
} // <- Test


int main()
{
  Test::Program::main();
}

"In testa che avete, Signor di Ceprano?"
-- Rigoletto

AnswerRe: C++ class question Pin
Daniel Pfeffer9-Dec-20 2:19
professionalDaniel Pfeffer9-Dec-20 2:19 
AnswerRe: C++ class question Pin
Richard MacCutchan9-Dec-20 4:43
mveRichard MacCutchan9-Dec-20 4:43 
GeneralRe: C++ class question Pin
pkfox9-Dec-20 5:53
professionalpkfox9-Dec-20 5:53 
GeneralRe: C++ class question Pin
Richard MacCutchan9-Dec-20 6:42
mveRichard MacCutchan9-Dec-20 6:42 
GeneralRe: C++ class question Pin
pkfox9-Dec-20 7:05
professionalpkfox9-Dec-20 7:05 
GeneralRe: C++ class question Pin
Richard MacCutchan9-Dec-20 7:10
mveRichard MacCutchan9-Dec-20 7:10 
GeneralRe: C++ class question Pin
pkfox9-Dec-20 12:02
professionalpkfox9-Dec-20 12:02 
QuestionDROPEFFECT_NONE does not work in conjunction with CF_HDROP Pin
Member 119022653-Dec-20 8:42
MemberMember 119022653-Dec-20 8:42 
QuestionRe: DROPEFFECT_NONE does not work in conjunction with CF_HDROP Pin
David Crow4-Dec-20 8:04
MemberDavid Crow4-Dec-20 8:04 
AnswerRe: DROPEFFECT_NONE does not work in conjunction with CF_HDROP Pin
Member 119022656-Dec-20 13:48
MemberMember 119022656-Dec-20 13:48 
QuestionNetserverenum function return false detail Pin
Member 141178533-Dec-20 0:43
MemberMember 141178533-Dec-20 0:43 
AnswerRe: Netserverenum function return false detail Pin
Richard MacCutchan3-Dec-20 1:06
mveRichard MacCutchan3-Dec-20 1:06 
GeneralRe: Netserverenum function return false detail Pin
Member 141178533-Dec-20 21:38
MemberMember 141178533-Dec-20 21:38 
GeneralRe: Netserverenum function return false detail Pin
Victor Nijegorodov3-Dec-20 21:42
MemberVictor Nijegorodov3-Dec-20 21:42 
GeneralRe: Netserverenum function return false detail Pin
Member 141178533-Dec-20 22:42
MemberMember 141178533-Dec-20 22:42 
GeneralRe: Netserverenum function return false detail Pin
Richard MacCutchan3-Dec-20 23:48
mveRichard MacCutchan3-Dec-20 23:48 
AnswerRe: Netserverenum function return false detail Pin
Randor 3-Dec-20 23:35
professional Randor 3-Dec-20 23:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.