nested-class

Nested type problem

浪子不回头ぞ 提交于 2019-12-29 01:38:05
问题 I just tried to create this simple implementation: class Test { private int abc = 0; public class TestClass { private void changeABC() { abc = 123; } } } If I compile it, it will complain: Cannot access a non-static member of outer type 'A.Test' via nested type 'B.Test.TestClass' I dont like the solution of setting: static int abc = 0; Is there any other solution for this? 回答1: You are probably coming from a Java background where this code would work as expected. In C#, nested types are

Passing pointer to member func of outer class template to nested class

寵の児 提交于 2019-12-24 17:04:10
问题 I'm having difficulty passing a pointer to the member function Outer<T>::foo to the constructor of the nested class Outer as shown below (see also ideone). template<typename T1> struct Outer { void foo() { } Outer() : inner( &Outer::foo ) // ERROR: compiles without &Outer::foo and Inner( F f ), below { } template<typename T2, void (T2::*F)()> struct Inner { Inner( F f ) // ERROR { } }; Inner<Outer,&Outer::foo> inner; }; int main() { Outer<int> outer; } What am I doing wrong? I've begun to

Extending a nested class method

岁酱吖の 提交于 2019-12-24 12:33:45
问题 I have encountered a virtual method in a nested class. ##classone.h class ClassOne: { public: class InnerClass{ public: virtual void method1(); ... ##classone.cpp void ClassOne::InnerClass::method1() { ... } I am subclassing ClassOne and need to extend method1() . What need's to be done with the nested class in that situation? What I tried ##subclassone.h class SubClassOne: public ClassOne{ public: virtual void method1(); ##subclassone.cpp void SubClassOne::InnerClass::method1() { ##New

Understanding partial specialization of inherited nested class templates

谁都会走 提交于 2019-12-24 07:46:00
问题 This question is connected to a previous Q&A in which a bug report for gcc was mentioned (supposedly fixed in gcc 4.5.0) and concerns some peculiarities for partial specialization of nested class template. My setup is that I have a class Base with a nested class template Inner that is partially specialized for char (using the dummy parameter trick, because explicit speciliaztion is not allowed in-class). #include <type_traits> #include <iostream> #include <ios> struct Base { // dummy template

How to refer to a global type from within a class that has a nested type with the same name?

对着背影说爱祢 提交于 2019-12-23 17:17:59
问题 I have a class declared at the global scope and another class with the same name that is nested within some class. class Address { var someProperty: String? } class ThirdPartyAPI { class Address { var someOtherProperty: String? init(fromAddress address: Address) { self.someOtherProperty = address.someProperty } } } The question is: how can I refer to a global class instead of the inner one from its initialiser? In the example given I've got an error Value of type 'ThirdPartyAPI.Address' has

How to call a method from a nested class, Java?

旧街凉风 提交于 2019-12-22 11:29:05
问题 How to call methodTwo(); from methodOne(); ? class Name { void methodOne() { class InnerClass { void methodTwo() { } } } } Thank You! 回答1: You need to create an instance of the InnerClass , in the same way as any other instance method needs an instance on which to call it: class Name { void methodOne() { class InnerClass { void methodTwo() { } } InnerClass x = new InnerClass(); x.methodTwo(); } } It's worth being careful before doing this - I don't think I've ever seen a named class declared

Overloading operator<< for a nested private class possible?

烈酒焚心 提交于 2019-12-22 03:53:09
问题 How one can overload an operator<< for a nested private class like this one? class outer { private: class nested { friend ostream& operator<<(ostream& os, const nested& a); }; // ... }; When trying outside of outer class compiler complains about privacy: error: ‘class outer::nested’ is private 回答1: You could make the operator<< a friend of outer as well. Or you could implement it completely inline in nested , e.g.: class Outer { class Inner { friend std::ostream& operator<<( std::ostream&

Get address of a non-POD object from within a data member, which is a single-use nested class

吃可爱长大的小学妹 提交于 2019-12-22 00:43:04
问题 I'll start with some code: class myNonPODClass { public: virtual ~myNonPODClass() {} class { public: myNonPODClass* GetContainer() { return (myNonPODClass*)((int8_t*)(this) - offsetof(myNonPODClass, member)); } } member; }; Obviously, this is a contrived example. The code compiles fine, but I'm worried about the "Offset of on non-POD type 'myNonPODClass'". Is there a better way to do essentially the same thing WITHOUT having to pass the myNonPODClass pointer into the nested anonymous classes

When is a static nested class (and static members therein) loaded into memory?

你。 提交于 2019-12-21 07:58:12
问题 Here, I was trying to implement a singleton class for my Database connectivity using the inner static helper class : package com.myapp.modellayer; public class DatabaseConnection { private DatabaseConnection() { //JDBC code... } private static class ConnectionHelper { // Instantiating the outer class private static final DatabaseConnection INSTANCE = new DatabaseConnection(); } public static DatabaseConnection getInstance() { return ConnectionHelper.INSTANCE; } } However, my doubt is when

Private nested static class - Good or bad practice?

不羁岁月 提交于 2019-12-21 07:06:20
问题 Would it be considered a bad practice to nest a private static class inside of a non-static class? public class Outer { private static class Inner { } } The idea here is that all instances of 'Outer' would share access to the static state. Another way to do it might be to just let the Inner class be non-static and use a static instance of it: public class Outer { private static innerInstance = new Inner(); private class Inner { } } Similar effect. What are the pros / cons or other