Struct Question

This is a discussion on Struct Question within the C++ Programming forums, part of the General Programming Boards category; Hey, What if I wanted a constructor that would take arguments to define its internals, but sometimes I didn't want ...

  1. #1
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407

    Struct Question

    Hey,

    What if I wanted a constructor that would take arguments to define its internals, but sometimes I didn't want to define the internals such as in a header.

    ex:
    Code:
    struct Vector3{
      Vector3(float x, float y, float z);
      float X, Y, Z;
    };
    Now that works as I had hoped to set all the varables in one statement, but in a class declaration I really don't want to set the variables until its constructor definition.

    I was told a long time ago to only declare in a .h and only define in .cpp so when doing something like.

    Code:
    class Camera{
      private:
        Vector3 Location;  // trying to not define here but my above example requires it
      public:
        Camera(Vector3 location); // or here
    };
    I know I'm missing something simple. Thanks for your time!

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Code:
    struct Vector3{
      Vector3();
      Vector3(float x, float y, float z);
      float X, Y, Z;
    };
    seemed to work by my compiler, but will this behave properly?

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    If I understand your question correctly, yes, your second iteration should work the way you expect. Also, it's not required to declare in .h and define in .cpp. In fact, you could write your entire project in either .h or .cpp with no problems. The only reason the two exist separately is to make code clearer, more organized, and easier to maintain. It's preferred that you use the two extensions as described, but definately not required.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,434
    Why would you wish to create a Vector with uninitialised members? The only thing you can usefully do with it is initialise the members (anything access of the value of an uninitialised member gives undefined behaviour).
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Why would you wish to create a Vector with uninitialised members? The only thing you can usefully do with it is initialise the members (anything access of the value of an uninitialised member gives undefined behaviour).
    Yea I realized I was leaving that hole open and that was where I was concerned about the method.

    I initialize everything a class owns inside its constructor religiously thats why initializing in the declaration was annoying me. I only want to adjust the code in one place if I want the defaults of that class to change rather than changing some in the .cpp and some in the .h.

    I am also passing large amounts of floats between functions and wrapping them up in Vectors Lets me use the same X, Y, Z description while still talking about many different things.

    Since the struct doesn't require you to initialize it I made the classes that own vectors require you to pass them values in their constructors.

    I realize I could probably also add a boolean called Initialized that is set as false in the constructor with no arguments, and set as true inside the constructor that initializes and then just check this boolean before accessing (but I figured that would be tedious so I stuck with the class requiring arguments in its constructor).

    There is probably a better method, but I think I closed off the hole for undefined behavior.

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Why would you wish to create a Vector with uninitialised members? The only thing you can usefully do with it is initialise the members (anything access of the value of an uninitialised member gives undefined behaviour).
    Yea I realized I was leaving that hole open and that was where I was concerned about the method.

    I initialize everything a class owns inside its constructor religiously thats why initializing in the declaration was annoying me. I only want to adjust the code in one place if I want the defaults of that class to change rather than changing some in the .cpp and some in the .h.

    I am also passing large amounts of floats between functions and wrapping them up in Vectors Lets me use the same X, Y, Z description while still talking about many different things.

    Since the struct doesn't require you to initialize it I made the classes that own vectors require you to pass them values in their constructors.

    I realize I could probably also add a boolean called Initialized that is set as false in the constructor with no arguments, and set as true inside the constructor that initializes and then just check this boolean before accessing (but I figured that would be tedious so I stuck with the class requiring arguments in its constructor).

    There is probably a better method, but I think I closed off the hole for undefined behavior.

    Code:
    Vector3::Vector3(){
      X = 0.0f;
      Y = 0.0f;
      Z = 0.0f;
    }
    also works.
    Last edited by Lesshardtofind; 12-29-2012 at 11:44 PM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,434
    Then why not use default arguments?
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Then why not use default arguments?
    Wouldn't I lose the functionality of being able to initialize a Vector3 within a function call that requires a Vector3?

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,434
    What do you think this does?
    Code:
    void SomeFunc(const Vector3 &);
    
    int main()
    {
         SomeFunc(Vector3());
    }
    This would work whether Vector3 has a constructor with no arguments, or it has multiple arguments each with a default value.
    Right 98% of the time, and don't care about the other 3%.

  10. #10
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    I think I lost you. I just read that c++11 allows non-static data member initializers, should I just assign the defaults in the declaration?

    I did all this to get around doing that as I had been told it was bad practice.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Unless you know very clearly and precisely what you have been informed is bad practice, and why, then just forget it.
    Half understood advice is often of less value than being ignorant of it.
    Just do what you want to do and post the code, then we can tell you exactly what is good or bad.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Unless you know very clearly and precisely what you have been informed is bad practice, and why, then just forget it.
    Half understood advice is often of less value than being ignorant of it.
    Alright so its not as big a deal as I treated it, though it has, over the years, become part of my coding style. Initializing every variable of a class inside it's constructor has as well.

    Just do what you want to do and post the code, then we can tell you exactly what is good or bad.
    Its a glut/openGL program with a couple hundred lines of code I really felt like it would be rude to request ya'll to read through hundreds of lines of code when the struct was the only thing in question.

    I also was worried someone would tell me that Glut wasn't purely native C++ code, but if you are curious I attached the source code. I put it all in one file with declarations at the top and definitions at the bottom.

    Requires: -lglut -lGLU and -std=c++11 to compile.
    Attached Files Attached Files

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    The way I understand the standard for this feature is that if non-static data member initialization is present, and constructor initialization is present, then the non-static data member initialization doesn't even take place. I have to admit not liking the feature very much, but the use that makes the most sense to me is when data members which are frequently initialized to the same things. You may need to provide a constructor where these members are conspicuously absent and another one where they are included so they can be called according to whatever whim. It isn't really about choosing one way or another way. What I would avoid is writing a default constructor if you were going to use this feature, because it is redundant.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Lesshardtofind View Post
    Hey,

    What if I wanted a constructor that would take arguments to define its internals, but sometimes I didn't want to define the internals such as in a header.
    This question makes absolutely no sense. What does a header have to do with anything?

    Now that works as I had hoped to set all the varables in one statement, but in a class declaration I really don't want to set the variables until its constructor definition.

    I was told a long time ago to only declare in a .h and only define in .cpp so when doing something like.

    Code:
    class Camera{
      private:
        Vector3 Location;  // trying to not define here but my above example requires it
      public:
        Camera(Vector3 location); // or here
    };
    I know I'm missing something simple. Thanks for your time!
    Neither place requires you do anything. The member variable declaration doesn't need an initializer (and until C++11, cannot have one), and the constructor declaration doesn't need one either.

    The constructor definition is a different matter, but you can put that in a .cpp file.
    Code:
    Camera::Camer(Vector3 location) : Location(location) {}
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Neither place requires you do anything. The member variable declaration doesn't need an initializer (and until C++11, cannot have one), and the constructor declaration doesn't need one either.
    My compiler begged to differ saying no constructor in Vector3 matches a constructor with 0 arguments, until I added the no argument constructor. (Maybe because I have -std=c++11?) As far as why? I explained that twice and Imalc answered that I was originally informed of something that did not matter much.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. Struct delcaring a struct within itself question
    By illidari in forum C Programming
    Replies: 3
    Last Post: 12-03-2010, 02:01 PM
  3. Struct question
    By Death_Wraith in forum C++ Programming
    Replies: 21
    Last Post: 06-03-2004, 05:22 PM
  4. Question about list (struct {..struct *next}
    By jmzl666 in forum C Programming
    Replies: 1
    Last Post: 10-23-2002, 01:34 AM
  5. struct question...
    By Cheeze-It in forum C Programming
    Replies: 3
    Last Post: 11-07-2001, 05:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21