Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Private inheritance question

Private inheritance question

Scheduled Pinned Locked Moved C / C++ / MFC
learningdatabasedata-structuresoophelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    marinme
    wrote on last edited by
    #1

    I'm trying to make a class using private inheritance from two template classes. I've made my own String class(I'm learning from a book and it has you do this), my own Array class. the Array class is a template class, and I am using it to take a type of another template class that takes two arguments. class Class_name: private Array< other_class >, private String { public: Class_name(int l = 10); ~Class_name(); void Set(int in = 0); //takes index for array private: const int len; //length of array }; the only definition that I think is relevant is the Set() method void Class_name::Set(int in) { //take input from user to pass to array } in this member function, I'm not sure how to call the template class type of the array's function. using containment, I can just set a variable of array type in it and call the function using the [] operator as a reference for the class... Thanks for your help, please let me know if I'm too vague

    B 1 Reply Last reply
    0
    • M marinme

      I'm trying to make a class using private inheritance from two template classes. I've made my own String class(I'm learning from a book and it has you do this), my own Array class. the Array class is a template class, and I am using it to take a type of another template class that takes two arguments. class Class_name: private Array< other_class >, private String { public: Class_name(int l = 10); ~Class_name(); void Set(int in = 0); //takes index for array private: const int len; //length of array }; the only definition that I think is relevant is the Set() method void Class_name::Set(int in) { //take input from user to pass to array } in this member function, I'm not sure how to call the template class type of the array's function. using containment, I can just set a variable of array type in it and call the function using the [] operator as a reference for the class... Thanks for your help, please let me know if I'm too vague

      B Offline
      B Offline
      Bob Ciora
      wrote on last edited by
      #2

      Actually, this isn't a question of inheritance, merely of scope. If you want to explicitly call Array's Set method, you simply define the call with the proper specifier:

      void Class_name::Set(int in)
      {
      // Store it to the Array...
      Array<other_class>::Set(in);
      }

      If you don't include the Array<other_class>:: specifier, the compiler wouldn't know any better, and would end up calling Class_name::Set. This would, of course, crash your stack soon enough. You'll find yourself doing this sort of thing a lot in C++, primarily in virtual functions that override the same function in the base class. If it's required to call the base class, then you have to explicitly define the base class specifier to force a call to the function in the base class. Hope it helps! Bob Ciora

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups