dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Question

  1. #1
    Join Date
    Mar 2018
    Posts
    37

    Question

    In my code, I used a global vector which a content of another vector will return in this global vector for using and calling in another part of my code.
    when the inheritance is used, where I must introduce a global vector? is it possible that I introduce it in the base

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,366

    Re: Question

    Quote Originally Posted by Kmilano View Post
    In my code, I used a global vector which a content of another vector will return in this global vector for using and calling in another part of my code.
    when the inheritance is used, where I must introduce a global vector? is it possible that I introduce it in the base
    Yes. A member of a base class can be accessed from a derived class (assuming access specifications allow). Also a static public member of a base class can be accessed from anywhere (although only one copy - not one per instance).

    Consider

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class B {
    public:
    	auto getb() {return b;}
    
    	static vector<int> sb;	// Declaration only. Needs definition
    
    protected:
    	vector<int> b = {3, 4};
    };
    
    class D : public B {
    public:
    	auto getd() {return b;}
    };
    
    vector<int> B::sb = {4, 5};	// Definition
    
    int main()
    {
    	D d;
    
    	for (const auto& e : d.getd())
    		cout << e << " ";
    
    	cout << endl << endl;
    
    	for (const auto& e : D::sb)
    		cout << e << " ";
    
    	cout << endl;
    }
    Last edited by 2kaud; April 28th, 2018 at 05:18 AM. Reason: Added example
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.1)

  3. #3
    Join Date
    Mar 2018
    Posts
    37

    Re: Question

    I used inheritance and object-oriented programming in c++ (visual studio)
    I have a base class and some derived classes.
    In the derived class, I defined some void function and inside them, one void function contained a vector for creating a matrix and other void function is contained a vector and some operation on 2 matrixes.
    and I need to use a content of this matrix inside the second void function in this derived class, so for calling a content of this matrix I used a global vector and then I returned a result of that matrix inside a global vector, after that in the second void function, I called a global vector (instead of the vector which contains a matrix),


    like the following example:

    derived class:
    Code:
    //======first void function===========
    void H::Mat(std::vector< std::vector<int> > &res){
    ....
    }
    ch=res;
    
    //======2nd void function============
    void H::multiplication(std::vector< std::vector<int> > &mul){
    int a[];
    ...
    mul= (ch[][]*a[]);
    
    }
    base class:
    Code:
    class base{
    protected:
    std::vector< std::vector<int> > ch;
    std::vector< std::vector<int> > res;
    std::vector< std::vector<int> > mul;
    
    public:
    virtual void H::Mat(std::vector< std::vector<int> > &res)=0
    virtual void H::multiplication(std::vector< std::vector<int> > &mul)=0
    }
    I would like to know that is it correct that I put that global vector( std::vector< std::vector<int> > ch inside a private part in the base class to call in the derived class?
    Last edited by Kmilano; April 28th, 2018 at 04:12 PM.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,366

    Re: Question

    ch, res and mul will be accessible to class base and its derived classes (assuming the derived classes are not inherited privately) - but not outside of these classes.

    PS.

    Code:
    mul= (ch[][]*a[]);
    Is an 'interesting' statement... but won't compile!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.1)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)