How to initialize inner class that has a c-tor with more than one parameter

S. Beniashvili 20 Reputation points
2024-06-18T14:25:23.7266667+00:00

Hi to all,

I'm trying to write a simple code and it won't compile!

Any ideas?

#include <stdio.h>

class RegClass {

public:

RegClass(int param, char * theName) { printf("c-tor (%d,%s)\r\n",  param, theName); } 

};

class Test {

public:

int a;

RegClass Reg { 5 , "a string" };

};

int main() {

Test t;

t.a = 5;

return 0;

}

Developer technologies | C++
{count} votes

2 answers

Sort by: Most helpful
  1. S. Beniashvili 20 Reputation points
    2024-06-18T14:45:55.67+00:00

    OK, solved!!!

    the correct line should be:

    RegClass(int param, const char * theName) { printf("c-tor (%d,%s)\r\n", param, theName); }

    0 comments No comments

  2. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2024-06-19T01:10:22.37+00:00

    Hi, @S. Beniashvili

    The error is due to /permissive- (Standards conformance) option.

    The /permissive- option sets the /Zc:referenceBinding, /Zc:strictStrings, and /Zc:rvalueCast options to conforming behavior.

    /Zc:strictStrings:

    const char * theName
    When specified, the compiler requires strict const-qualification conformance for pointers initialized by using string literals.

    User's image

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.