Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ class question

C++ class question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
18 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • pkfoxP pkfox

    Hi and thanks for replying but the problem I have is I don't know the path to my input file until later in the program

    "I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP

    Mircea NeacsuM Offline
    Mircea NeacsuM Offline
    Mircea Neacsu
    wrote on last edited by
    #7

    In that case have a default constructor for initialization and add a function BillingDates::Open (std::string& file) that gets called when you have the path to your file. You might also need something like bool BillingDates::IsOpen() const to check the state of the object.

    Mircea

    1 Reply Last reply
    0
    • pkfoxP pkfox

      Ok C++ is not mt my main language so please bear with me. For my question I will use this class

      class BillingDates
      {
      public:
      BillingDates(std::string InputFile);
      void ProcessDates();
      rapidjson::Document doc;
      std::string InputFile;
      std::string YearStartDate;
      std::string YearEndDate;
      std::string BBFDate;
      std::string BillIssueDate;
      std::string BillIssueDateLong;
      int CurrentFinancialYear = 0;
      std::ifstream InputStream;
      };

      And in my main program I declare a pointer and an object to the class thus

      BillingData *bData;
      BillingData bData2;

      I can then create my pointer so

      bData = new BillingDates("path to my file");

      how can I create an instance for bData2 ? I know I can do it like this

      BillingData bData2("path to my file");

      but that will create a new var and not use my previously declared var. Hope this makes sense

      "I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP

      J Offline
      J Offline
      Joe Woodbury
      wrote on last edited by
      #8

      If they need to be the same instance, versus a copy, consider using std::shared_ptr for both.

      1 Reply Last reply
      0
      • pkfoxP pkfox

        Ok C++ is not mt my main language so please bear with me. For my question I will use this class

        class BillingDates
        {
        public:
        BillingDates(std::string InputFile);
        void ProcessDates();
        rapidjson::Document doc;
        std::string InputFile;
        std::string YearStartDate;
        std::string YearEndDate;
        std::string BBFDate;
        std::string BillIssueDate;
        std::string BillIssueDateLong;
        int CurrentFinancialYear = 0;
        std::ifstream InputStream;
        };

        And in my main program I declare a pointer and an object to the class thus

        BillingData *bData;
        BillingData bData2;

        I can then create my pointer so

        bData = new BillingDates("path to my file");

        how can I create an instance for bData2 ? I know I can do it like this

        BillingData bData2("path to my file");

        but that will create a new var and not use my previously declared var. Hope this makes sense

        "I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #9

        Time to invest some time (pardon the pun) on learning smart pointers. See, for instance unique_ptr, shared_ptr, weak_ptr, scoped_ptr, raw pointers - Knowing your smart pointers (2/7) - Fluent C++[^].

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

        In testa che avete, signor di Ceprano?

        pkfoxP 1 Reply Last reply
        0
        • pkfoxP pkfox

          Ok C++ is not mt my main language so please bear with me. For my question I will use this class

          class BillingDates
          {
          public:
          BillingDates(std::string InputFile);
          void ProcessDates();
          rapidjson::Document doc;
          std::string InputFile;
          std::string YearStartDate;
          std::string YearEndDate;
          std::string BBFDate;
          std::string BillIssueDate;
          std::string BillIssueDateLong;
          int CurrentFinancialYear = 0;
          std::ifstream InputStream;
          };

          And in my main program I declare a pointer and an object to the class thus

          BillingData *bData;
          BillingData bData2;

          I can then create my pointer so

          bData = new BillingDates("path to my file");

          how can I create an instance for bData2 ? I know I can do it like this

          BillingData bData2("path to my file");

          but that will create a new var and not use my previously declared var. Hope this makes sense

          "I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP

          D Offline
          D Offline
          Daniel Pfeffer
          wrote on last edited by
          #10

          In C++, you can define variables at any point, not just at the beginning of the block. Define bData2 only at the point that file name is known.

          Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

          1 Reply Last reply
          0
          • CPalliniC CPallini

            Time to invest some time (pardon the pun) on learning smart pointers. See, for instance unique_ptr, shared_ptr, weak_ptr, scoped_ptr, raw pointers - Knowing your smart pointers (2/7) - Fluent C++[^].

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

            pkfoxP Offline
            pkfoxP Offline
            pkfox
            wrote on last edited by
            #11

            Hi there, I don't think I'm explaining what is confusing me so I'll try again. If I define a class object in a C# program I can instantiate it like so Sample of the top of my head code

            namespace Test
            {
            public class Dummy
            {
            string Path {get;set;}
            Public Dummy(string Path)
            {
            this.Path = Path;
            }
            }

            class Program
            {
                Dummy dummy {get;set;}
                \[STAThread\]
                static void Main()
                {
                    Program p = new Program();
                    p.dummy = new Dummy("MyPath");
                }
            }
            

            }

            In C++ I can only create it like this if I make the dummy variable a pointer ( which is not a problem but I'd like to know how to do it the other way) I'm sure this must be doable. Thanks for everyones help so far.

            "I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP

            CPalliniC 1 Reply Last reply
            0
            • pkfoxP pkfox

              Ok C++ is not mt my main language so please bear with me. For my question I will use this class

              class BillingDates
              {
              public:
              BillingDates(std::string InputFile);
              void ProcessDates();
              rapidjson::Document doc;
              std::string InputFile;
              std::string YearStartDate;
              std::string YearEndDate;
              std::string BBFDate;
              std::string BillIssueDate;
              std::string BillIssueDateLong;
              int CurrentFinancialYear = 0;
              std::ifstream InputStream;
              };

              And in my main program I declare a pointer and an object to the class thus

              BillingData *bData;
              BillingData bData2;

              I can then create my pointer so

              bData = new BillingDates("path to my file");

              how can I create an instance for bData2 ? I know I can do it like this

              BillingData bData2("path to my file");

              but that will create a new var and not use my previously declared var. Hope this makes sense

              "I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #12

              Correcting the mis-spellings you have:

              BillingDates *bData; // the class is called BillingDates not BillingData
              BillingDates bData2;

              In the above code bData is a pointer that has not yet been initialised, so it does not actually point to anything. bData2 is an actual instance of the class, or would be if you had included the no-parameters constructor to get it to compile. But I am not sure what you are trying to do with them. You could add the following line of code at some point:

              bData = new BillingDates("someFilename");

              but then what? And it is still not clear what bdata2 is supposed to be for.

              pkfoxP 1 Reply Last reply
              0
              • L Lost User

                Correcting the mis-spellings you have:

                BillingDates *bData; // the class is called BillingDates not BillingData
                BillingDates bData2;

                In the above code bData is a pointer that has not yet been initialised, so it does not actually point to anything. bData2 is an actual instance of the class, or would be if you had included the no-parameters constructor to get it to compile. But I am not sure what you are trying to do with them. You could add the following line of code at some point:

                bData = new BillingDates("someFilename");

                but then what? And it is still not clear what bdata2 is supposed to be for.

                pkfoxP Offline
                pkfoxP Offline
                pkfox
                wrote on last edited by
                #13

                Hi Richard, sorry about the spelling mistakes I'm actually working on multiple project so that was a cut'n past jobbie I know I can do this

                bData = new BillingDates("someFilename");

                but how can I create an instance of the class using bData2 ( not in the declaration of the variable I know how to do that ) as I said earlier in C# ypu can declare like

                someclass someclassob {get;set;}

                Then create the instance

                someclassobj = new someclass("Parameter goes here");

                even though you didn't specify a parameter in the declaration of someclassobj .

                "I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP

                L 1 Reply Last reply
                0
                • pkfoxP pkfox

                  Hi Richard, sorry about the spelling mistakes I'm actually working on multiple project so that was a cut'n past jobbie I know I can do this

                  bData = new BillingDates("someFilename");

                  but how can I create an instance of the class using bData2 ( not in the declaration of the variable I know how to do that ) as I said earlier in C# ypu can declare like

                  someclass someclassob {get;set;}

                  Then create the instance

                  someclassobj = new someclass("Parameter goes here");

                  even though you didn't specify a parameter in the declaration of someclassobj .

                  "I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #14

                  pkfox wrote:

                  ypu can declare like

                  someclass someclassob {get;set;}

                  That's a new one on me, I must look it up. As I mentioned you need to add a parameterless constructor to the class, and some method that allows you to add a filename at runtime. something like:

                  class BillingDates
                  {
                  public:
                  BillingDates();
                  BillingDates(std::string InputFile);
                  void addFile(std::string InputFile);
                  void ProcessDates();
                  // stuff removed for readability
                  };
                  // the implementation of addFile will do the same as the constructor that takes a filename.

                  You can then write:

                  BillingDates *bData;
                  BillingDates bData2;

                  // and later on something like
                  bData2.addFile("your file name here");
                  bData = &bData2;

                  But I really don't know why you need any of that. All you really need is:

                  BillingDates *bData;

                  // and later on something like

                  bData = new BillingDates("your file name here");

                  Does that make (any) sense?

                  pkfoxP 1 Reply Last reply
                  0
                  • L Lost User

                    pkfox wrote:

                    ypu can declare like

                    someclass someclassob {get;set;}

                    That's a new one on me, I must look it up. As I mentioned you need to add a parameterless constructor to the class, and some method that allows you to add a filename at runtime. something like:

                    class BillingDates
                    {
                    public:
                    BillingDates();
                    BillingDates(std::string InputFile);
                    void addFile(std::string InputFile);
                    void ProcessDates();
                    // stuff removed for readability
                    };
                    // the implementation of addFile will do the same as the constructor that takes a filename.

                    You can then write:

                    BillingDates *bData;
                    BillingDates bData2;

                    // and later on something like
                    bData2.addFile("your file name here");
                    bData = &bData2;

                    But I really don't know why you need any of that. All you really need is:

                    BillingDates *bData;

                    // and later on something like

                    bData = new BillingDates("your file name here");

                    Does that make (any) sense?

                    pkfoxP Offline
                    pkfoxP Offline
                    pkfox
                    wrote on last edited by
                    #15

                    Hi Richard yes it does and in fact is what I'm currently doing - I think I need to take my C# head off - thanks very much for your time

                    "I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP

                    L 1 Reply Last reply
                    0
                    • pkfoxP pkfox

                      Hi Richard yes it does and in fact is what I'm currently doing - I think I need to take my C# head off - thanks very much for your time

                      "I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #16

                      I do think going backwards from C# to C++ is going to mess with your head quite a lot. Good luck.

                      pkfoxP 1 Reply Last reply
                      0
                      • pkfoxP pkfox

                        Hi there, I don't think I'm explaining what is confusing me so I'll try again. If I define a class object in a C# program I can instantiate it like so Sample of the top of my head code

                        namespace Test
                        {
                        public class Dummy
                        {
                        string Path {get;set;}
                        Public Dummy(string Path)
                        {
                        this.Path = Path;
                        }
                        }

                        class Program
                        {
                            Dummy dummy {get;set;}
                            \[STAThread\]
                            static void Main()
                            {
                                Program p = new Program();
                                p.dummy = new Dummy("MyPath");
                            }
                        }
                        

                        }

                        In C++ I can only create it like this if I make the dummy variable a pointer ( which is not a problem but I'd like to know how to do it the other way) I'm sure this must be doable. Thanks for everyones help so far.

                        "I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP

                        CPalliniC Offline
                        CPalliniC Offline
                        CPallini
                        wrote on last edited by
                        #17

                        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 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 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 #include 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;

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

                          cout << p.dummy->get() << "\\n";
                        
                          // no cleanup needed
                        }
                        

                        };
                        } // <- Test

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

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

                        In testa che avete, signor di Ceprano?

                        1 Reply Last reply
                        0
                        • L Lost User

                          I do think going backwards from C# to C++ is going to mess with your head quite a lot. Good luck.

                          pkfoxP Offline
                          pkfoxP Offline
                          pkfox
                          wrote on last edited by
                          #18

                          :-D

                          "I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups