A pointer question.

This is a discussion on A pointer question. within the C++ Programming forums, part of the General Programming Boards category; Hi, all I have a question about the pointer and here is my header file and cpp file. Code: // ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    77

    A pointer question.

    Hi, all

    I have a question about the pointer and here is my header file and cpp file.

    Code:
    // Integer.h
    #ifndef INTEGER_H
    #define INTEGER_H
    
    class Integer
    {
    public:
    	Integer() { value = 0;} // set value is 0 in the default constructor
    	
    	Integer( int intVal ); // create a value "intVal" in this constructor
       
    	~Integer(); // Destructor
    	
    	int getInteger() const; 
    	
    	void setInteger( int newInteger );
    	
    	Integer& operator=( const Integer& rhInteger );
    
    private:
    	int* value; //replace from int value;
    };
    
    #endif
    and cpp file
    Code:
    // Integer.cpp
    #include <cstdlib>
    #include "integer.h"
    
    Integer::Integer( int intVal)
    {
    	value = intVal;
    }
    
    int Integer::getInteger() const
    {
    	return value;
    }
    
    void Integer::setInteger( int newInteger )
    {
    	value = newInteger;
    }
    
    Integer& Integer::operator=( const Integer& rhInt )
    {
    	Integer copy(rhInt);
    	int* temp = value;
    	value = copy.value; //steal the new deep copy
    	copy.value = temp;
    	return *this;
    
    }
    However, when i try to compile it, it has 3 errors.

    c:\documents and settings\misia\my documents\homework\cosc2406\pointers\integer.cpp(7 ) : error C2440: '=' : cannot convert from 'int' to 'int *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast


    c:\documents and settings\misia\my documents\homework\cosc2406\pointers\integer.cpp(1 2) : error C2440: 'return' : cannot convert from 'int *const ' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast


    c:\documents and settings\misia\my documents\homework\cosc2406\pointers\integer.cpp(1 7) : error C2440: '=' : cannot convert from 'int' to 'int *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    Error executing cl.exe.

    Can someone give me some advises how to correct it?
    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    I also want to know what it is for
    Code:
    Integer& operator=( const Integer& rhInteger );
    Integer is the class name?
    and why should i need to use & after Integer?
    Also operator= is for what?
    and I also confuse about ( const Integer& rhInteger);

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    First things first. An int variable can store an integer:

    int x = 100;

    and the only variable that can store an integer is an int variable. So, a variable that is of type pointer to int:

    int* value;

    cannot store an integer. A variable of type "pointer to int" can store the address of an integer, which works like this:

    int x = 100;
    int* value = &x;

    In that context, the '&' symbol is called the "address of operator", and it fetches the address in memory where the variable is located. So, a variable of type "pointer to int" stores the address of a location in memory where an int is located.

    Integer is the class name?
    Yes.
    and why should i need to use & after Integer?
    In that context, the '&' symbol means the variable is of type "reference to Integer". If the parameter is not declared as a reference type(or a pointer), the parameter will be passed by value, which is inefficient, and inside the function, you will not be able to change the original object if you need to. If you pass an object by value, the object will be copied, and the copy will be passed to the function, so any changes you make will be to the copy. Then, when the function ends, the copy will be destroyed leaving the original object unchanged.
    Also operator= is for what?
    To define what you want the equal sign to do with two of your objects. For instance, if your objects have four member variables, you could define equals to mean: copy only the first member; or copy the first two members; or multiply the members by 2 and then copy all of them to the other object. If you don't define your own operator= method, then the compiler will supply a default operator= for you. The default operator= will copy each member variable from the object on the right hand side of the equals sign to the corresponding member variable of the object on the left hand side of the equals sign. That may be fine for your needs, however the default operator= can cause problems if you have member variables that are initialized with dynamically allocated memory, for instance:
    Code:
    SomeClassConstructor()
    {
    	member1 = new int[3];
    	...
    	...
    }
    In that case, the default operator= will copy the address in member1 from the object on the right hand side of the equals sign to member1 of the object on the left hand side of the equals sign. The result will be that both objects have a member1 that points to the same location in memory, and if one object subsequently changes the value at that location in memory, it will also change for the other object's member1 since it also points to that location--which is probably not what you want. Therefore, you will usually define operator= for your objects if you use the new operator to dynamically reserve memory for member variables.

    and I also confuse about (const Integer& rhInteger);
    1) Integer rhInteger -- is an Integer object named rhInteger, which stands for "right hand Integer", in other words: the Integer object on the right hand side of the equals sign.

    2) Integer& rhInteger -- changes the type from Integer to "reference to Integer", which means the original object will not be copied when it is passed to the function. The effect of that is:
    a) it is more efficient, and
    b) it will allow you to permanently change the original object inside the function.

    3) const Integer& rhInteger -- by adding const, it means the function will not change the object being passed to the function, negating part of the effect of 2).
    Last edited by 7stud; 03-21-2005 at 09:02 AM.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    thanks so much about that, it's quite useful for me.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    Hi 7stud,

    I have one question.. will const Integer& null the effect of Integer& partly or totally? U said partly..? Can you please explain it a little more?

    Also, then how will integral types like Integer be converted into pointer types like Integer& ?
    PLEASE REPLY. PLEASE HELP. It's urgent for me.

    Thanks.

    --Rohit

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I have one question.. will const Integer& null the effect of Integer& partly or totally? U said partly..? Can you please explain it a little more?
    When you pass a regular variable into a function, only it's value get's passed-in. So if X=2, the value "2" get's passed-in. You can change the value of X inside the function, and the original X is still equal to 2.

    On the other hand, when you pass-in a reference to X, the function can change the value of the original X. But, delcaring it constant means you can't change it...

    Also, then how will integral types like Integer be converted into pointer types like Integer& ?
    You don't normally want to convert a pointer (the address of a variable) into a regular variable. Normally, you just want to access the variable via a pointer. (You can use cast to convert one type to another, but C++ tries to prevent you from doing it accidently.)

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,659
    Actually, this was already answered in another thread.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Hi 7stud,
    You do realize this thread is three years old (give or take a day)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 09:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 01:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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