in Education by
I need to cast a class to its protected base: class ComplicatedOne : public Object { //Lots of funcs I don't want or need. }; class Line : protected ComplicatedOne { //Some funcs of ComplicatedOne get re-implemented or called by alias here }; class Array { void Add (Object &obj); }; main() { Array a; a.Add(new Line()); } I need to add a Line to an Array but the Object base is not visible. Should I re-impliment its methods publicly or is there an implicit cast operator I could override? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
With this, you're telling the compiler that you can't implicitly convert a Line to an Object: class Line : protected Object { }; But it seems to me that you want to do this, and also that you should do this. So make the inheritance public. This is a design question. Don't make the inheritance protected just to keep methods in the base class protected. One other option is to implement the cast operator in Line: class Line : protected Object { public: operator Object&(); }; and call the function like so: a.Add(Line()); instead of a.Add(new Line()); You can't implicitly cast pointers in this situation. However I suggest changing the inheritance type.

Related questions

0 votes
    What is a base class and derived class?...
asked Dec 31, 2020 in Technology by JackTerrance
0 votes
    Which is a valid declaration within an interface? A)protected short stop = 23; b)final void madness(short stop); ... (double duty);? Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    Which of these class allows us to get real time data about private and protected member of a class? (a) java. ... & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Can a class be declared with a protected modifier. (a) True (b) False This question was posed to ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    A class member declared protected becomes a member of subclass of which type? (a) public member (b) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    A class member declared protected becomes a member of subclass of which type? (a) public member (b) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    NAND is called as _gate a) fundmental gate b)derived gate c) logical gate d)electronic gate Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    NAND is called as _gate a) fundmental gate b)derived gate c) logical gate d)electronic gate Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    If we need to inherit from a base class and want to pass something from the constructor of the inherited class to the constructor of the base class, how can we do that?...
asked Jan 16, 2021 in Technology by JackTerrance
0 votes
    Mention whether NSObject is a parent class or derived class?...
asked Nov 10, 2020 in Technology by JackTerrance
0 votes
    I ran into a problem a few days ago when I had to introduce C++ files into a Java project. It ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I ran into a problem a few days ago when I had to introduce C++ files into a Java project. It ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I ran into a problem a few days ago when I had to introduce C++ files into a Java project. It ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
+1 vote
    What is the meaning of base address of the array in C-Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
...