Thread: Some syntax issues - C++

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    135

    Some syntax issues - C++

    Hello everyone!

    1. If I'm declaring some static array as a member of a class, for example:

    Code:
    Matrix matrices[4];
    Now, suppose I have a constructor which receives an array 4 matrices to assign to that member.

    Now isn't it right to define the elements of our matrices member this way, for example:
    Code:
    matrices[0](matricesFromCont[0]); // Assuming we have a copy constructor in Matrix class
    What's the problem with that?

    2. Another, maybe related question:
    Why isn't it correct to pass as an argument the following one:
    Code:
    const Matrix& arr[]
    Thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by HelpMeC
    What's the problem with that?
    That looks like an attempt to call an overloaded operator() on matrices[0], passing matricesFromCont[0] as an argument. It has nothing to do with a copy constructor unless the overloaded operator() has a parameter that results in copy construction.

    Quote Originally Posted by HelpMeC
    Why isn't it correct to pass as an argument the following one:
    If that's what you have in mind as an actual argument... the syntax is simply invalid.
    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

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @laserlight
    Tha first issue - got it.

    The second one - So, how can one to pass such an argument?
    Code:
    const Matrix arr[]
    But by reference, as we don't want to create another copy...

    Thank you!!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What are you trying to pass as an argument?
    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
    Nov 2019
    Posts
    135
    @laserlight -
    In this particular case, we know that we have to receive an array of 4 matrices (Matrix object) actually, so maybe the right form of writing it, is such that:
    Code:
    const Matrix arr[4]
    But, this way the array is passing as a copy (to my understanding) where I want it to be passed by reference; I want to avoid copying...

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ohhh... you want to pass an array as an argument. An array is converted to a pointer to its first element when passed as an argument. This rule remains true even when the array elements are objects of class type.

    So, what you want is a pointer parameter, more specifically a pointer to const parameter, along with a second parameter to specify the number of elements. Or, you might use a std::initializer_list.

    Or, it might make more sense to have four parameters if you really need exactly four matrices.
    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

  7. #7
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @laserlight -
    Oh yea, C -> C++ - this is actually a pointer so there is no any copy except for the address of the first object...

    BTW, the copy constructor is invoked only when the name of the class is followed by the instance?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by HelpMeC
    BTW, the copy constructor is invoked only when the name of the class is followed by the instance?
    No. Think about things like parameters.
    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

  9. #9
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @laserlight
    You mean some cases like:
    Code:
    void func(const Matrix m) {}
    But in this case when we are passing some matrix into this function, it performs the assignment operator on it, doesn't it?

    Thanks.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Write a program to find out.
    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

  11. #11
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @laserlight -
    Yea, according to the PPT of my class - it does invoke the the copy constructor (the default one or the implemented one).
    Strange language

    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with what syntax is best
    By Leepox in forum C Programming
    Replies: 5
    Last Post: 08-20-2013, 10:03 AM
  2. Array Syntax Versus Pointer Syntax
    By LyTning94 in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2011, 10:56 AM
  3. C++ Syntax
    By skrep in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2011, 10:30 PM
  4. Syntax issues..
    By Shamino in forum C++ Programming
    Replies: 14
    Last Post: 07-03-2007, 04:32 PM
  5. syntax help
    By Neoground1 in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2002, 03:37 AM

Tags for this Thread