0

I have a quick question while reading a source code. AFAIK, the name of a constructor should be identical to the class declared. But, the following code has a different name as a constructor. Anybody can tell me why this code works?

class directFieldMapper
:
    public FieldMapper
{
    const labelUList& directAddressing_;

    bool hasUnmapped_;

public:

    // Constructors

        //- Construct given addressing
        patchFieldSubset(const labelUList& directAddressing)
        :
            directAddressing_(directAddressing),
            hasUnmapped_(false)
        {
            if (directAddressing_.size() && min(directAddressing_) < 0)
            {
                hasUnmapped_ = true;
            }
        }

    //- Destructor
    virtual ~directFieldMapper()
    {}
}

Ok, I found that this class is not in the source code list in the Makefile. So, this class is never compiled.

6
  • 1
    patchFieldSubset is not a constructor for directFieldMapper. Commented Feb 6, 2018 at 16:41
  • That, or a typedef (maybe? not sure), or an evil macro. Commented Feb 6, 2018 at 16:41
  • 5
    "why this code works?" this code does not work, at least as shown here there will be a couple of compiler errors unrelated to the actual issue. Can you provide a minimal reproducible example? Commented Feb 6, 2018 at 16:44
  • 1
    Could patchFieldSubset be a nested class inside directFieldMapper, and you just missed/accidentally omitted the relevant code? Indentation suggests that that could be possible. Commented Feb 6, 2018 at 16:45
  • 2
    This does not compile. Commented Feb 6, 2018 at 16:46

1 Answer 1

5

Is it possible to have constructor have different name as the class declared?

No.

AFAIK, the name of a constructor should be identical to the class declared.

Correct.

But, the following code has a different name as a constructor.

The code is ill-formed according to C++ standard.

Anybody can tell me why this code works?

If it works, then it is due to some language extension. You can consult the manual of your compiler about language extensions it provides, and how to enable / disable them.

Being ill-formed, all compilers are required to issue a diagnostic message, or else they are not compliant. Example output from the GNU compiler:

error: ISO C++ forbids declaration of 'patchFieldSubset' with no type [-fpermissive]

         patchFieldSubset(const labelUList& directAddressing)

                                                            ^

warning: non-static reference 'const labelUList& directFieldMapper::directAddressing_' in class without a constructor [-Wuninitialized]

     const labelUList& directAddressing_;

                       ^~~~~~~~~~~~~~~~~

error: expected ';' after class definition

 }
  ^
Sign up to request clarification or add additional context in comments.

1 Comment

I would mention that constructors cannot have different names because constructors have no names at all, accordingly with the standard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.