c++ - Member Initialization in overloaded constructor -


i have basic semantic related doubt couldn't able clarified. before stating problem write down minimum code explain clearly.

in constants.hpp

#pragma once enum class openglesversion{     oes1,     oes2 }; 

in cone.hpp, have enum member

openglesversion esversion; 

overloaded constructor definition in cone.cpp takes enum parameter

cone::cone(const openglesversion version) : rotationangle(0), scale(1), esversion(version) {} 

now in renderingengine.cpp when instantiate cone object:

cone cone(openglesversion::oes2); 

i syntax error:

no type named 'oes2' in 'openglesversion'

now if update object instantiaition to:

cone cone{openglesversion::oes2}; 

now works.

my complier libc++[llvm c++ standard library c++ 11 support] ,and dialect c++11[-std=c++11] using xcode.

now coming doubt: went through resources clarify concept. in past had use pre c++11 version in school following bjarne's c++ programming book printed in 2008, that's why didn't find "curly" initialization syntax on book c++11 came out later.

i tried google , wasn't sure of keyword search later common subject name pointed "list initialization". every resource covered on advantage of new syntax in c++11 never got clarified on why normal round bracket treated syntax error because there many resource out there example shows using rounder bracket combination of curly braces while explaining difference using examples of primitive types like:

int x(4); int x{4}; 

so again not clarifying concept me. here know or have guidance appropriate link or resource explain doubt.

  • has compiler made round bracket intialization syntax obsolete?
  • it's not obsolete never supported in c++?
  • i have declared , defined overloaded constructor wrongly?
  • is initialiation of member not supported via round bracket syntax? , why?

edit: happens overloaded version, lets int parameter overloaded version of constructor.

cone::cone(const int foovariable) : foomember(foovariable){} 

only code works is:

cone cone{8}; 

i get

expected parameter declarator, expected ')'

for cone cone(8);

this behaviour doesn't happen if curiosity instantiate inside member function of cone class compiler doesn't thow syntax or error:

void cone::test(){     cone cone(openglesversion::oes2); } 

edit update 2: think have test case should narrow down. here pattern:

class renderingengine { private:     cone cone(openglesversion::oes2); }; 

so declaring , instantiating cone object member of renderingengine class. causing issue? because if do

cone cone(openglesversion::oes2); 

inside member function of renderingengine/outside class implementation works. there must basic rule violating on , how non-primitive member variable can declared , instantiated.

member initialization has been introduced in c++11.

parenthesis not supported avoid vexing parse, can use {..} or = syntax:

class renderingengine { private:     cone cone{openglesversion::oes2}; }; 

or

class renderingengine { private:     cone cone = cone(openglesversion::oes2); // =, can use parent on right side }; 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -