Passing object references to functions

This is a discussion on Passing object references to functions within the C++ Programming forums, part of the General Programming Boards category; Hello all, I had posted a code regarding operations of matrices leading to a function using these matrix operations iteratively. ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    28

    Passing object references to functions

    Hello all,
    I had posted a code regarding operations of matrices leading to a function using these matrix operations iteratively. Here's the post for reference,
    Compile time of C versus C++ code

    I have implemented the changes to the functions:
    Code:
    matrix operator+(const matrix &mat2) const ;
    matrix operator-(const matrix &mat2) const ;
    friend matrix operator*(const matrix &mat1, const matrix &mat2);
    friend matrix operator*(const matrix &mat1, float cnst);
    friend matrix operator*(float cnst, const matrix &mat1);
    matrix operator=(const matrix &mat2);
    friend matrix inverse(const matrix &mat1);
    friend matrix zeros(int x, int y);
    friend matrix identity(int x);
    friend void mat_show(const matrix &mat1);
    I have a few conceptual questions. In the function declarations, how is "const matrix &mat2" interpreted? Does the sequence of the declaration move from right to left i.e matrix &mat2 is an object to be passed as a reference parameter and then this reference object is declared as a const?

    Also, in the first two function declarations, does the const at the end of the declaration after the parameter list result in the return object (a local object called temp declared in the function) converted to a const value before returning?

    Thanks.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    Look at it this way. This is how I do:
    First, we have the type.
    Then, we have a specifier, ie pointer * or reference &.
    Then we have the name.
    So, const matrix is the type.
    & is the specifier, reference.
    And mat1 is the name.
    So, it's a reference to a const matrix object named mat1.

    Const at the end of a function implies that the function itself will not modify any class members or global variables (I think?). It's a way to tell the compiler that the function won't modify the state of the instance of the class. It will only work with local variables.
    But I did mention it can modify local variables, so it can return a non-const variable. Note that if it couldn't, then the compiler would enforce const matrix as return type, but it doesn't, so it's ok.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    Thanks Elysia.

    I also found this very interesting link about the use of const:
    http://www.parashift.com/c++-faq-lit...rrectness.html

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Const at the end of a function implies that the function itself will not modify any class members or global variables (I think?).
    No, it implies that the member function will not modify any member variables (unless those members have been declared mutable, in which case the const member function is expected to preserve the logical state of the object, but might not do so (which is immoral, of course) ). Global variables can still be modified in the body of a const member function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    Sorry if I sound like a dumbass but I just started coding in C++ and am taking time figuring out some stuff.

    Suppose I make a change in the code:

    Code:
    matrix matrix::operator=(const matrix &mat2);
    to
    Code:
    matrix matrix::operator=(matrix &mat2);
    i.e I am removing the const from the parameter being passed to the function. I get errors at all the places where:
    <Object of type matrix>=<Expression involving object of type matrix>

    e.g:
    Code:
    k1=dt*(linv*u-linv*r*i);
    Is this because the overloaded functions "*","+" etc return an local object temp of class matrix? Since this object does not exist outside the function, what is returned to the R.H.S of the code above is a const object of class matrix. Therefore, must the "=" overloaded function must have a const matrix & in the parameter list? An answer to this might help me understand how these objects are dealt with.

    This is the code for the "*" operator by the way.
    Code:
    matrix operator*(const matrix &mat1, const matrix &mat2) {
    	int x, y, z;
    	float sum;
    	matrix temp;
    
    // Checking for compatibility - columns of left matrix equal to rows of right matrix.
    	if (mat1.columns!=mat2.rows) {
    		cout << "Matrices are not compatible.\n";
    		exit(1);
    	}
    
    // Performing multiplication.
    	for (x=0;x<mat1.rows;x++) {
    		for (z=0;z<mat2.columns;z++) {
    			sum=0.0;
    			for (y=0;y<mat1.columns;y++) {
    				sum=sum+mat1.mat_var[x][y]*mat2.mat_var[y][z];
    			}
    			temp.mat_var[x][z]=sum;
    		}
    	}
    
    // Defining dimensions of the product.
    	temp.rows=mat1.rows;
    	temp.columns=mat2.columns;
    
    	return temp;
    }
    Thanks.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The operators return temporaries, and you can't bind temporaries to non-const references.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    Thanks Elysia, Laserlight and CornedBee.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 04:13 PM
  2. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 02:21 PM
  3. Passing functions between files
    By Duskan in forum C Programming
    Replies: 9
    Last Post: 04-17-2007, 08:44 AM
  4. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 06:48 PM
  5. Really Hope I'm not Overposting (Object passing)
    By Shamino in forum Game Programming
    Replies: 10
    Last Post: 12-16-2005, 09:38 AM

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