class problem in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stromhau
    New Member
    • Sep 2006
    • 12

    class problem in c++

    Hi!

    I am a beginner in c++ programming. How can i say that my class will use another class but at that point i will not invoke the constructor. Will invoke the constructor when some arguments are ready.

    if i say at the beginning of the program(want this object to be global)

    Surface surf ; // problem is that the constructor takes two ints but i havent calculated them yet.

    Tommy
  • m013690
    New Member
    • Sep 2006
    • 23

    #2
    You would need to create a default constructor that takes no arguments, OR you could put default arguments into the constructor you do have, i.e.

    in your header file

    surf::surf ( int = 0, int = 0 );

    then in the .cpp file

    surf::surf ( int x, int y ) {
    // set your class variables to these values
    }

    what happens is that you can call the constructor with two variables, or none, and if you call it with none, it will use the defaults you gave it in the header file ( 0 and 0 ).

    You won't be able to call the constructor at a later time, but you could always build in a set function to use later so that you can set those variables when you know them.

    That help?

    Comment

    • stromhau
      New Member
      • Sep 2006
      • 12

      #3
      Yes it helped a lot :)

      I only belived it was a smarter way of doing it.

      Comment

      • m013690
        New Member
        • Sep 2006
        • 23

        #4
        Originally posted by stromhau
        Yes it helped a lot :)
        Great, glad to help. That's what these forums are for.

        Comment

        Working...