using directive.

This is a discussion on using directive. within the C++ Programming forums, part of the General Programming Boards category; I ran across something curious in C++. I have always associated the using directive with namespaces, but this one I ...

  1. #1
    Registered User
    Join Date
    May 2007

    Question using directive.

    I ran across something curious in C++. I have always associated the using directive with namespaces, but this one I have not encountered until now.

    I saw something like the following code in my work and have not found an explanation of why the code works as it does.

    #include <stdio.h>
    class test1
        virtual void afunct() =0;
    class test2 : public test1
        void afunct(void);
    class test3 : protected test2
       void bfunct(void);
    	using test2::afunct;
    void test2::afunct(void)
    void test3::bfunct(void)
    void main()
       test2 v1;
       test3 v2;
    I compile and run it and it works. Comment out the using directive and afunct() is not accessible through class test3. Part of my confusion may be due to the fact that I never really got the hang of protected.

    I am just curious as to how/why it works the way it does.

  2. #2
    Registered User
    Join Date
    Sep 2009
    Because test3 uses protected inheritence to inherit from test2, the afunct function is protected in test3 and, therefore, the main function cannot access it directly.

    The using directive changes the access of the inherited function afunct to public, because it is under the public: label, allowing main to access it.

    At least, I think that's how it works.

  3. #3
    Registered User
    Join Date
    May 2007
    O.k., found it. C++ programmers guide, pg 407. It is as Ushakal says, the using directive makes the function publicly available.


  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    By the way, that is a using declaration, not a using directive.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 10:13 AM
  2. Replies: 6
    Last Post: 08-12-2009, 05:46 AM
  3. macro directive question
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 05-23-2008, 03:47 AM
  4. Warning on an ifndef directive
    By DavidP in forum C++ Programming
    Replies: 2
    Last Post: 08-02-2007, 02:31 AM
  5. Replies: 2
    Last Post: 05-15-2007, 04:30 AM

Tags for this Thread

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21