0

I want to use references from the base class without initializing them in the derived class. Is that possible? If so, how?

The example code below generates an error, i.e.line 28: error #368: "Base::Base()" provides no initializer for: reference member "Base::BaseVar1" reference member "Base::BaseVar2"

#include <stdio.h> class Base { protected: int & BaseVar1; int & BaseVar2; public: Base() = default; Base(int & BaseVar1, int & BaseVar2) : BaseVar1(BaseVar1),BaseVar2(BaseVar2){} virtual void vfunction1(void) = 0; }; class Derived1: public Base { public: Derived1(int & Derived1Var1): Derived1Var1(Derived1Var1) { } int & Derived1Var1; void vfunction1() { printf("BaseVar1 = \n",BaseVar1); printf("BaseVar2 = \n",BaseVar2); printf("Derived1Var1 = \n",Derived1Var1); } }; class Derived2: public Base { public: Derived2(int & Derived2Var1): Derived2Var1(Derived2Var1) { } int & Derived2Var1; void vfunction1() { printf("BaseVar1 = \n",BaseVar1); printf("BaseVar2 = \n",BaseVar2); printf("Derived2Var1 = \n",Derived2Var1); } }; 
22
  • 2
    A reference cannot be left "uninitialized". If you actually need that, use a pointer. Commented Apr 20, 2022 at 15:19
  • 4
    The whole derived class is a red herring. Your base class alone needs to initialize the member reference variables in the consructor. Commented Apr 20, 2022 at 15:21
  • 1
    remove the Base() constructor and call the other Base constructor from within the initialization list of your derived constructors. Commented Apr 20, 2022 at 18:16
  • 1
    Actually Derived1(int & Derived1Var1, int & BaseVar1FromDerived1, int & BaseVar2FromDerived1): Base(BaseVar1FromDerived1, BaseVar2FromDerived1), Derived1Var1(Derived1Var1) Commented Apr 20, 2022 at 18:22
  • 1
    I can put it into an answer later. Commented Apr 20, 2022 at 18:42

1 Answer 1

2

As some commenters mentioned, references have to be initialized (in the class definition or their member definition or in the constructor).

The default constructor of Base does not do that, so it should be removed.

Your derived classes can call the other Base constructor from within the initialization list in their constructors.

#include <cstdio> // C++ variant of stdio.h, which puts symbols in std using namespace std; // do not use this in headers (in your production projects) class Base { protected: int& BaseVar1; int& BaseVar2; public: // Base() = default; // removed Base(int& BaseVar1, int& BaseVar2) : BaseVar1(BaseVar1), BaseVar2(BaseVar2) {} virtual void vfunction1() = 0; }; class Derived1: public Base { public: Derived1(int& BaseVar1FromDerived1, int& BaseVar2FromDerived1, int& Derived1Var1) : Base(BaseVar1FromDerived1, BaseVar2FromDerived1), Derived1Var1(Derived1Var1) { } int& Derived1Var1; void vfunction1() override // signify override { printf("BaseVar1 = %d\n", BaseVar1); // %d was missing printf("BaseVar2 = %d\n", BaseVar2); // %d was missing printf("Derived1Var1 = %d\n", Derived1Var1); // %d was missing } }; class Derived2: public Base { public: Derived2(int& BaseVar1FromDerived2, int& BaseVar2FromDerived2, int& Derived2Var1) : Base(BaseVar1FromDerived2, BaseVar2FromDerived2), Derived2Var1(Derived2Var1) { } int& Derived2Var1; void vfunction1() override // signify override { printf("BaseVar1 = %d\n", BaseVar1); // %d was missing printf("BaseVar2 = %d\n", BaseVar2); // %d was missing printf("Derived2Var1 = %d\n", Derived2Var1); // %d was missing } }; int main() { int a = 1; int b = 2; int c = 3; Derived2 d2(a, b, c); d2.vfunction1(); } 

Output:

BaseVar1 = 1 BaseVar2 = 2 Derived2Var1 = 3 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.