代码之家  ›  专栏  ›  技术社区  ›  Filip Frącz

C++与CLI和朋友程序集的怪异行为

  •  1
  • Filip Frącz  · 技术社区  · 14 年前

    怎么回事?我为什么要这么做?

    代码:

    // Assembly 1 "FriendAssemblyBase"
    namespace FriendAssemblyTest
    {
        public ref class BaseClass
        {
        internal:
            BaseClass()
            {
            }
    
            virtual void DoSomething()
            {
            }
        };
    }
    
    // Allow "FriendAsseblyDerived" to see internals
    [assembly:  ::System::Runtime::CompilerServices::InternalsVisibleToAttribute(
    "FriendAssemblyDerived, PublicKey=0024000004800000940000000602000000240000525341310004000001000100a95fe809ecc53c3a826aa32e3ab1309f2ecae4b91dc649457d704e150f3a4007a151ffe28852f947803dd1a1586c5c0ae2c9688bd76299857ee65ff7efb14905e03b33664a42e2fa1074080c3bad971623514ab6dd9fc4e4343ba7fb98884ecce45f96b71e5f1b55de88e36483274aa71c740fdfb14aacaada6ca22cb39bf9a2")];
    
    // ----------------
    // Assembly 2. "FriendAssemblyDerived". Strongly named and signed
    #using "FriendAssemblyBase.dll" as_friend
    namespace FriendAssemblyTest
    {
    
        public ref class Derived : BaseClass
        {
        public:
            Derived() {}
    
            void Foo()
            {
                DoSomething();
            }
    
        internal: // <-- Causes C3252 (cannot reduce visibility of virtuals)
            virtual void DoSomething() override
            {
            }
        };
    }
    
    1 回复  |  直到 14 年前
        1
  •  0
  •   Ben Voigt    14 年前

    因为其他班级 FriendAssemblyBase 可以打电话 BaseClass.DoSomething .

    Liskov替换原则说,任何可以用基类完成的事情都必须对每个派生类都是可行的。做 DerivedClass.DoSomething FriendsassemblyBase公司 public: .