Thread: Passing object references to functions

  1. #1
    Registered User
    Join Date
    Feb 2008

    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,

    I have implemented the changes to the functions:
    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?


  2. #2
    Join Date
    Oct 2007
    Inside my computer
    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
    Thanks Elysia.

    I also found this very interesting link about the use of const:

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2008
    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:

    matrix matrix::operator=(const matrix &mat2);
    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>

    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.
    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";
    // Performing multiplication.
    	for (x=0;x<mat1.rows;x++) {
    		for (z=0;z<mat2.columns;z++) {
    			for (y=0;y<mat1.columns;y++) {
    // Defining dimensions of the product.
    	return temp;

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

    "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
    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, 03:13 PM
  2. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  3. Passing functions between files
    By Duskan in forum C Programming
    Replies: 9
    Last Post: 04-17-2007, 07: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