Class Constructors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    Class Constructors

    Hi I have a question about classes. If you have a class and its constructor takes arguements. Is there a way you can create an array that stores objects from that class but each one of them getting diefferent arguements?

    Btw, I'm using it to create a game using SDL, tetris and I have a class fo blocks and for the pieces and I want the pieces class to have an array that stores the blocks in its piece.

    Thanks in advance.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    No if you create an array of classes either as automatic variables or by allocating then they all call the default constructor.

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #3
      Originally posted by Banfa
      No if you create an array of classes either as automatic variables or by allocating then they all call the default constructor.
      So you're saying that if I create an array of instances they all have to be passed the same arguments?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by ilikepython
        So you're saying that if I create an array of instances they all have to be passed the same arguments?
        No I am saying you can't pass any arguments, you have to use the default constructor, that is a constructor that takes no arguments or where all arguments have a default value.

        The workaround for your problem is to have a default constructor tat does very little and then to have an Initialise member function that takes the arguments and initialises the class data from them. Then you construct your array and then call the initialise member function for each array item.

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by Banfa
          No I am saying you can't pass any arguments, you have to use the default constructor, that is a constructor that takes no arguments or where all arguments have a default value.

          The workaround for your problem is to have a default constructor tat does very little and then to have an Initialise member function that takes the arguments and initialises the class data from them. Then you construct your array and then call the initialise member function for each array item.
          Oh ok then thanks very much.

          Comment

          • joeschnell
            New Member
            • Apr 2007
            • 47

            #6
            Hey Python I'm looking for my post on classes and ran across yours. Looks like your a term ahead of me, are you a GSP major? I have a post here somewhere and it deals with classes and constructors, having written our first program using structs now we're into classes, seemingly quick too. Joe..

            Good luck, sounds confusing.

            Comment

            • joeschnell
              New Member
              • Apr 2007
              • 47

              #7
              What do you do when you've forgotten what you named your thread and you can't find it? i'd appreciate it

              <snipped>
              Last edited by Banfa; Aug 15 '07, 11:50 AM. Reason: Removed email address

              Comment

              • ilikepython
                Recognized Expert Contributor
                • Feb 2007
                • 844

                #8
                Originally posted by joeschnell
                Hey Python I'm looking for my post on classes and ran across yours. Looks like your a term ahead of me, are you a GSP major? I have a post here somewhere and it deals with classes and constructors, having written our first program using structs now we're into classes, seemingly quick too. Joe..

                Good luck, sounds confusing.
                Well, I''m not in college and I have no idea what a GSP major is. I didn't learn this with school, I just did it on my own.

                Comment

                • ilikepython
                  Recognized Expert Contributor
                  • Feb 2007
                  • 844

                  #9
                  Originally posted by joeschnell
                  What do you do when you've forgotten what you named your thread and you can't find it? i'd appreciate it

                  <snipped>
                  You go to your profile and click on "find all threads started by joeschnell" and it will take you to a list of threads you made.

                  Also, please don't post your email, it's against the posting guidelines.

                  Comment

                  Working...