Asigment and subscript operator

This is a discussion on Asigment and subscript operator within the C++ Programming forums, part of the General Programming Boards category; Hello, I am creating a template class called Array which manages an array of pointers to pointers... this is the ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    9

    Asigment and subscript operator

    Hello, I am creating a template class called Array which manages an array of pointers to pointers...

    this is the problem, suppose the Array class is created to type double, and I want to do this:

    object_of_type_Array[1] = pointer_to_doble_var;

    how do I program the assigment opeator so that it knows to put it in index #1 the pointer to the varible of type double??

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    After a quick google search I got this link for ya which explains it quite well.

    http://www.devarticles.com/c/a/Cplus...g-in-C-plus/1/

    or if you're too lazy to look:

    Code:
    var_type& operator[](int column)
    Is the overloading function you need to use.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    9
    yes, I know about operator overloading, but the thing is that in all examples i've found the assigment operator takes on only an object of the same type that the class...

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    *stares blankly*

    *blinks*

    ....Use a void* maybe?

    *blinks again*

    *walks away*

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by rusty0412
    yes, I know about operator overloading, but the thing is that in all examples i've found the assigment operator takes on only an object of the same type that the class...
    Not at all. An overloaded assignment operator can take any type you specify.
    Code:
    class YourClass {
       //...
       YourClass& operator=(YourClass&) //default assignment
       YourClass& operator=(int) //takes an int
       YourClass& operator=(double) //takes a double
    };
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

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