Arrays and constructors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andy

    #1

    Arrays and constructors

    Hello,

    I have created a class called shape, I've then called the following
    code:

    shape myShape;

    This then calls the default constructor for this class:

    Shape::Shape( void )
    {
    OtherClass vertices[3];
    }
    >From now on I'm guessing what should be happening. This code passes an
    array called vertices to the default constructor of OtherClass(this
    constructor simple sets a few double variables). My question is how do
    access the contents of the array (vertices[0], vertices[1],
    vertices[2]) within the main() function.

    This is code that I currently have:

    cout << myShape.get_val ue();

    Any suggestions?

  • hardi.pertel@gmail.com

    #2
    Re: Arrays and constructors


    andy wrote:
    Hello,
    >
    I have created a class called shape, I've then called the following
    code:
    >
    shape myShape;
    >
    This then calls the default constructor for this class:
    >
    Shape::Shape( void )
    {
    OtherClass vertices[3];
    }
    >
    From now on I'm guessing what should be happening. This code passes an
    array called vertices to the default constructor of OtherClass(this
    constructor simple sets a few double variables). My question is how do
    access the contents of the array (vertices[0], vertices[1],
    vertices[2]) within the main() function.
    >
    This is code that I currently have:
    >
    cout << myShape.get_val ue();
    >
    Any suggestions?
    The constructor is only called when you create a variable of type
    Shape. But maybe it's possible to access those variables (vertices)
    when they are static
    they will be erased if they are not static(this is your only chance)

    Comment

    • Rolf Magnus

      #3
      Re: Arrays and constructors

      andy wrote:
      Hello,
      >
      I have created a class called shape, I've then called the following
      code:
      >
      shape myShape;
      >
      This then calls the default constructor for this class:
      >
      Shape::Shape( void )
      {
      OtherClass vertices[3];
      }
      >
      >>From now on I'm guessing what should be happening. This code passes an
      array called vertices to the default constructor of OtherClass(this
      constructor simple sets a few double variables).
      The above code doesn't do that. It creates within the Shape constructor an
      array of three instances of OtherClass and calls that array 'vertices'.
      My question is how do access the contents of the array (vertices[0],
      vertices[1], vertices[2]) within the main() function.
      The array is local to the constructor. It ceases to exist once the
      constructor returns.

      Comment

      • hardi.pertel@gmail.com

        #4
        Re: Arrays and constructors

        The array is local to the constructor. It ceases to exist once the
        constructor returns.
        Thus making it static could help to access it later

        Comment

        • Ron Natalie

          #5
          Re: Arrays and constructors

          andy wrote:
          >
          Shape::Shape( void )
          {
          OtherClass vertices[3];
          }
          >
          >>From now on I'm guessing what should be happening. This code passes an
          array called vertices to the default constructor of OtherClass(this
          constructor simple sets a few double variables).
          No it doesn't it creates a three element array of OtherClass (default
          constructed) local to the Shape constructor functino.
          My question is how do
          access the contents of the array (vertices[0], vertices[1],
          vertices[2]) within the main() function.
          You can't. vertices is a local variable. As soon as Shape's
          constructor returns it is gone.

          If you want an array to be CONTAINED inside Shape, you should
          declare it there. For example:

          class Shape
          OtherClass vertices[3];
          public:
          Shape(); // your defaut constructor
          };
          >
          This is code that I currently have:
          >
          cout << myShape.get_val ue();
          >
          You could write one to return the internal object. However,
          you're going to have to tell me what it is that you are trying
          to do. What do you want output? If you feed vertices to
          cout<< it's just going to convert it to void* and print the
          address ( not very interesting).


          Comment

          • Salt_Peter

            #6
            Re: Arrays and constructors


            hardi.pertel@gm ail.com wrote:
            The array is local to the constructor. It ceases to exist once the
            constructor returns.
            >
            Thus making it static could help to access it later
            Making it static would destroy the project. Thats like saying that all
            and any points share the same 3 coordinates regardless of location. And
            besides, static does not "help" access, it complicates it.

            Comment

            • Salt_Peter

              #7
              Re: Arrays and constructors


              andy wrote:
              Hello,
              >
              I have created a class called shape, I've then called the following
              code:
              >
              shape myShape;
              >
              This then calls the default constructor for this class:
              >
              Shape::Shape( void )
              {
              OtherClass vertices[3];
              }
              The above creates a temporary array. You could argue that it doesn't
              exist and i wouldn't be surprised that the compiler optimizes it away.
              >
              From now on I'm guessing what should be happening. This code passes an
              array called vertices to the default constructor of OtherClass(this
              constructor simple sets a few double variables). My question is how do
              access the contents of the array (vertices[0], vertices[1],
              vertices[2]) within the main() function.
              >
              This is code that I currently have:
              >
              cout << myShape.get_val ue();
              >
              Any suggestions?
              I think you are barking up the wrong tree. A 3D point can be used to
              create a vertex. A shape can hold a container of vertexes - say
              std::vector< Vertex (ie: a triangle has 3 of these). But an abstract
              shape does not have a fixed number of vertices. It seems to me that
              what you are describing above is a Point3D, not a Shape.

              And if what you are planning to argue, insofar as the Shape is
              concerned, is its coordinate location:

              I'm seeing something like so:

              #include <iostream>
              #include <iterator>

              class Point3D {
              double x;
              double y;
              double z;
              public:
              Point3D(double x_ = 0.0, double y_ = 0.0, double z_ = 0.0)
              : x( x_ ), y( y_ ), z( z_ ) { }
              friend std::ostream&
              operator<<( std::ostream& os, const Point3D& r_pt)
              {
              os << "pt.x = " << r_pt.x;
              os << " pt.y = " << r_pt.y;
              os << " pt.z = " << r_pt.z;
              return os << std::endl;
              }
              };

              class Shape {
              Point3D location;
              public:
              Shape() : location() { }
              Shape( const Point3D& loc ) : location( loc ) { }
              void move( const Point3D& loc ) { location = loc; }
              const Point3D getLocation() { return location; }
              };

              int main()
              {
              Shape shape;
              Point3D pt = shape.getLocati on();
              std::cout << pt;

              shape.move(Poin t3D(1.0,1.0,0.0 ));
              std::cout << shape.getLocati on();

              return 0;
              }

              /*
              pt.x = 0 pt.y = 0 pt.z = 0
              pt.x = 1 pt.y = 1 pt.z = 0
              */

              Comment

              • andy

                #8
                Re: Arrays and constructors


                The under lining reason why I need to do this is to utilize the
                constructor of one class in a second class - thereby optimizing the
                code by avoiding duplication.

                Take this example:

                Constructor of first class:

                Class1::Class1( void )
                {
                a = 0;

                }

                Instead of duplicating this I would like Class2 to use this same
                constructor:

                Class2::Class2( void )
                {
                //I would like code here that would pass an array to the Class1
                constructor and if the array was 5 in size then each position in the
                array would be initialized with a=0
                }

                I hope this is a little clearer ?

                Comment

                • Julián Albo

                  #9
                  Re: Arrays and constructors

                  andy wrote:
                  The under lining reason why I need to do this is to utilize the
                  constructor of one class in a second class - thereby optimizing the
                  code by avoiding duplication.
                  (...)
                  //I would like code here that would pass an array to the Class1
                  constructor and if the array was 5 in size then each position in the
                  array would be initialized with a=0
                  }
                  I hope this is a little clearer ?
                  Forget optimization until you know some more C++. You can't have a clue
                  about optimizing array usage if you don't know how to use an array.

                  --
                  Salu2

                  Comment

                  • Salt_Peter

                    #10
                    Re: Arrays and constructors


                    andy wrote:
                    The under lining reason why I need to do this is to utilize the
                    constructor of one class in a second class - thereby optimizing the
                    code by avoiding duplication.
                    >
                    Take this example:
                    >
                    Constructor of first class:
                    >
                    Class1::Class1( void )
                    {
                    a = 0;
                    >
                    }
                    >
                    Instead of duplicating this I would like Class2 to use this same
                    constructor:
                    >
                    Class2::Class2( void )
                    {
                    //I would like code here that would pass an array to the Class1
                    constructor and if the array was 5 in size then each position in the
                    array would be initialized with a=0
                    }
                    >
                    I hope this is a little clearer ?
                    Yes, its clearer except that you've missunderstood one fundamental
                    isse, with a std::vector, the code would be both much easier to
                    implement and (surprise!) a std::vector is faster than a primitive
                    array.

                    I can achieve the entire code below with 1 line in main using a
                    std::vector. Yes: just one line.
                    std::vector< double vn(11.1, 5);
                    Except, of course, that the vector is dynamic, resizeable, reserveable.

                    Note that you can't template Class1 if you choose to have
                    floating-points because a floating literal constant can't be in a
                    template parameter list. Something else a std::vector has no issues
                    with.

                    #include <iostream>
                    #include <stdexcept>

                    class Class1 {
                    double d; // modify to your convenience
                    public:
                    Class1() : d(11.1) { // default - set it to whatever you need
                    std::cerr << "Class1()\n ";
                    }
                    friend std::ostream&
                    operator<<( std::ostream& os, const Class1& r_c1 ) {
                    return os << r_c1.d;
                    }
                    };

                    // T is your target type
                    // Size is the array's injected size
                    template < typename T, const size_t Size >
                    class Container {
                    T array[ Size ];
                    public:
                    Container() { }
                    // size_t size() const
                    size_t size() const {
                    return Size;
                    }
                    // at(index)
                    T& at( const size_t index ) {
                    if ( Size <= index ) // bounds check
                    throw std::range_erro r(
                    "Container index out of range."
                    );
                    return array[ index ];
                    }
                    };

                    int main() {
                    try {
                    Container< Class1, 5 container;

                    for ( size_t i = 0; i < container.size( ); ++i ) {
                    std::cout << "container[" << i << "] = ";
                    std::cout << container.at( i ); // using accessor at()
                    std::cout << std::endl;
                    }

                    } catch ( const std::exception & r_e ) {
                    std::cerr << "error: ";
                    std::cerr << r_e.what() << std::endl;
                    }
                    }

                    /*
                    Class1()
                    Class1()
                    Class1()
                    Class1()
                    Class1()
                    container[0] = 11.1
                    container[1] = 11.1
                    container[2] = 11.1
                    container[3] = 11.1
                    container[4] = 11.1
                    */

                    Comment

                    Working...