-4

I have a copy constructor T::T(const T&). The object has two properties, let's say color and height. This means I need to assign the color and the height from the object in argument to my object. Problem is I don't know how to call the argument because it is not named.

If the argument is named, let's say t, code looks like this:

T::T(const T& t) {
    color = t.color
    height = t.height
}

But in my case there's no t argument. What should I replace the question mark ? with in the following code:

T::T(const T&) {
    color = ?.color
    height = ?.height
}

Thanks for help!

Marco E
  • 93
  • 1
  • 6
  • 6
    The real problem is the lack of a name for your parameter. What is stopping you from giving it a name? (You apparently can edit the function definition.) – JaMiT Feb 01 '20 at 22:20
  • 4
    That just makes no sense. Why would you not provide an argument name when you clearly need (and use) it? – Some programmer dude Feb 01 '20 at 22:20
  • Unlike many other languages (like for example Javascript) there's no universal "args" or similar variable. If you need to access an argument, you need to explicitly declare it as a formal argument in the *definition*. – Some programmer dude Feb 01 '20 at 22:22
  • 3
    Random guess: You saw the definition of a copy ctor being `T::T(const T&)` and now think it's forbidden to give the parameter a name? No need to worry, just name the parameter and use it as in your first example. – dtell Feb 01 '20 at 22:22
  • 1
    Putting together mine and dtell's comment, it seems you're confused about the difference between function *declarations* and *definitions*. in C++ you can omit argument variable names in declarations, and even definitions (implementations) if you want. But if you need to access an argument in the definition (implementation) of a function you *must* give that argument an explicit name. – Some programmer dude Feb 01 '20 at 22:25
  • 1
    Does that mean that declaration is T::T(const T&) and that I can implement that with T::T(const T& t)? – Marco E Feb 01 '20 at 22:29
  • @MarcoAndréEcheverria Yes that's correct. In *declarations* of functions (class-members or not) you don't have to provide argument names). – Some programmer dude Feb 01 '20 at 22:32
  • Thanks a lot! It worked! I've been trying to figure this out since a couple of hours now. – Marco E Feb 01 '20 at 22:35
  • @MarcoAndréEcheverria • In my _declarations_ I only provide the dummy parameter names for documentation purposes. Like `void DrawAt(int x, int y) const;` where the `x` and `y` help disambiguate their uses. – Eljay Feb 01 '20 at 22:50

2 Answers2

4

It is impossible without naming the parameter.

When you omit the parameter name, you do it because you don't need it. If you do need it, don't omit it.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
3

As @some-programmer-dude mentioned, I was confused about the difference between function declarations and definitions.

My declaration was T::T(const T&) but that didn't stop me from defining an argument t in my definition T::T(const T& t).

Thanks!

Marco E
  • 93
  • 1
  • 6