member functions having the class's name

This is a discussion on member functions having the class's name within the C++ Programming forums, part of the General Programming Boards category; Hi, I know that the classname as a function is the constructor, and with a ~ it's a destructor. But ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    71

    member functions having the class's name

    Hi,
    I know that the classname as a function is the constructor, and with a ~ it's a destructor. But what happens when one of the member functions is called the classname? And I also don't quite get it when a class's object is a pointer.. I'd really appreciate it if someone explained these two (with an example)..

    Linette

  2. #2
    Unregistered
    Guest
    I don't understand the first part of your question, but the second I think I can answer. Declaring an instance (object) of a particular class to be a pointer, is to make use of the Free Store (Heap RAM). Object pointers give great flexability to your program, because Heap RAM allocation is "dynamic", it occurs while your program is running. This is especially beneficial for programs like Air Traffic Control software. You do not know exactly how many airplane objects will be visible on a radar scope at any given moment when your writing the code for the program, Heap allocation with object pointers solves this problem...

    class Airplane { ... };

    Airplane * p1 = new Airplane; // dynamic Heap allocation...

    p1 now points to an airplane object on the Heap (a RAM address)

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421

    Post

    You can't have a member function with the same name as the class... it's just not allowed. If you have a function by that name, it has to be a constructor.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    You can have a function with the same name as the class name.. ok, can someone please explain this 'ArrayC1D' function:

    PHP Code:
    class ArrayC1D  {
       public:
          
    ArrayC1D (int *s);
          ~
    ArrayC1D ();
          
    void input (char *msg);
          
    ArrayC1D *transfer (ArrayC1D *old);
          
    change (int indexint value);
          
    int add ();
       private:
          
    int *a;
          
    int *size;
    };

    ArrayC1D::ArrayC1D (int *s) {  //constructor
       
    size s;
       
    = new int [*s];
    }

    ArrayC1D::~ArrayC1D () {  //destructor
       
    delete [] a;
    }

    void ArrayC1D::input (char *msg) {
       
    //here goes the body of void input
    }

    ArrayC1D ArrayC1D::transfer (ArrayC1D *old) {
       
    int i;
       for (
    i=0i<*sizei++)
          *
    a++ = *old->a++;  
       
    - *size  //reset array to start
       
    old->old-> - *size;
       return 
    this;
    }

    ArrayC1D::change (int indexint value) {
       
    [index] = value;
    }

    int ArrayC1D::add () {
       
    //here goes the body of add

    My other question is that how come with other functions you either say void or their datatype, but with 'change' (see above) you don't say anything?


    Thanks for your help,
    Linette

  5. #5
    Unregistered
    Guest
    Greetings,

    ArrayC1D *transfer (ArrayC1D *old);
    ^^^^^^ This is not the name of the function! Its the return type!
    the actual name is "transfer". This function returns a pointer to an ArrayC1D object.

    If you dont specify a return type it defaults to int.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    Hi,
    I put that code up to ask how that function (transfer) works.. That wasn't my example of having a function with the same name as the class (it's another question). What I need with that code is just an explanation as to how that function works.. It gets a parameter of ArrayC1D type, and its return type is that too.. But what do those variables (or objects) contain? In other words, I don't get it when you use the class name as a datatype.. (I'm new to classes) Could you please explain?

    Thanks a lot.
    Linette

  7. #7
    Unregistered
    Guest
    Any member function of a class that has the exact same name as the class itself is a constructor for that class. There may be multiple constructors for a class. As long as the argument list passed into the various constructors are differentiable from the point of view of the compiler, it will know which one you are trying to call when you are creating your instances of the class object.

  8. #8
    Unregistered
    Guest
    You create classes to create new data types...

    The transfer method in this ArrayC1D class requires both a pointer to an ArrayC1D object to be passed to it, and to be returned from it.

    This particular class can create objects that manipulate other objects created by this same class...

  9. #9
    Unregistered
    Guest
    ArrayC1D objects can "communicate" with each other through their transfer() methods.

    But for these objects to be able to do this, they must be created at runtime on the Heap, since transfer() requires pointers to ArrayC1D objects...

    ArrayC1D* obj = new ArrayC1D(intPointer);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Pointers to Class Member Functions
    By Dark_Phoenix in forum C++ Programming
    Replies: 6
    Last Post: 09-02-2007, 02:21 PM
  3. Pointers to member functions using virtual methods
    By Will B-R in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2006, 06:59 AM
  4. How do you call member functions
    By Elite in forum C++ Programming
    Replies: 7
    Last Post: 08-05-2003, 01:03 PM
  5. Reference another class's data member...
    By RoD in forum C++ Programming
    Replies: 4
    Last Post: 02-15-2003, 10:20 AM

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