Back in the classes again... I'm BAAAAACK!

Pages: 12
May 16, 2020 at 2:53am
Okay trying to pass a struct nested in a class nested in a class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class BaseClass
{
public:
    class SubClass
    {
        struct item01
        {
            int var01 = 1;
            int var02 = 2;
        };
    };
};

void printIt(BaseClass& cInput)
{
    std::cout << cInput.var01;
}


int main()
{
    BaseClass::SubClass::item01 myItem;
    printIt(myItem);
    return 0
}
May 16, 2020 at 3:09am
you have to initialize each object. nested objects need initialized directly after they are declared outside of their own scope.

1
2
3
4
5
6
7
struct a {
	struct b {

	};
	b bb;
};
May 16, 2020 at 3:53am
Why did you put b bb and not just b?
May 16, 2020 at 4:17am
Because b is a type. Just putting b would be like saying

1
2
3
struct S {
  int
};
May 16, 2020 at 9:36pm
You are taking a reference to a BaseClass, but you are passing in a BaseClass::SubClass::item01. Obviously that won't work. Furthermore, your item01 class is private in the SubClass (by default) so you woudn't even be able to instantiate it in line 22.
May 17, 2020 at 12:44am
I guess initialized its probably the wrong word. Declared is more accurate.

But myitem would just be declared as BaseClass myitem;

That is true that item01 is private.
Last edited on May 17, 2020 at 12:44am
May 17, 2020 at 1:07am
But myitem would just be declared as BaseClass myitem;


That would make myitem a member of BaseClass, not a member of BaseClass::SubClass::item01 like OP is looking for. BaseClass has no member named 'var01' so this code would fail to compile anyways unless the parameter type is changed from BaseClass& to a BaseClass::SubClass::item01&
May 17, 2020 at 1:38am
It's pretty hard to understand why someone would want a variable that is only a member of a nested object. I doubt that the op even knows what he was trying to do. I went through a similar progression trying to understand how nested objects worked.

Unless you're going for balls out performance kinda a waste of effort not just declaring a BaseClass object and just doing whatever you want to do with the variables inside the item01. If they're private they can only be modified by member functions anyways. According to the posted code they might aswell not even exist. I'd imagine they'd be optimized out if compiled
Last edited on May 17, 2020 at 1:39am
May 17, 2020 at 7:01pm
It's pretty hard to understand why someone would want a variable that is only a member of a nested object. I doubt that the op even knows what he was trying to do. I went through a similar progression trying to understand how nested objects worked.


There are many reasons why someone would want to create an object of a nested class. Consider the following example:

1
2
3
4
5
6
7
8
9
10
11
template<typename T, size_t dimensions, size_t... some_additional_args>
struct Grid
{
    Grid() = delete; //Cannot make a Grid<T, size_t, size_t...> object.
    template<size_t FirstSize, size_t... Sizes>
    struct DimensionSize///...but you can make a Grid::DimensionSize object.
    {
        std::vector<typename Grid<T, dimensions-1>::template DimensionSize<Sizes...>> vec{FirstSize};
    };
};
//And then code for the partial specialization for a 1D grid follows, etc. 


To instantiate the Grid, one must do the following:
1
2
3
4
//3D int grid with dimension sizes 10x20x5, like a 10x20x5 cube.
typename Grid<int,3, 1,2,3,4,5 /*some random args for internal stuff*/>
    ::template DimensionSize<10, 20, 5> myGrid;


Of course, the interface could provide a template alias to make that declaration easier. Here we see that the Grid class cannot be constructed at all (default constructor is deleted), and we can only access the class within Grid, which is DimensionSize. We define this because there are two variadic parameter packs and we need to be able to differentiate between them.

In this case, the Grid template basically acts more like a smarter namespace than a class, since it cannot be instantiated. It only serves to hold that variadic argument list in the beginning.

Unless you're going for balls out performance kinda a waste of effort not just declaring a BaseClass object and just doing whatever you want to do with the variables inside the item01. If they're private they can only be modified by member functions anyways.


No, the class item01 ITSELF is private within the context of SubClass, because the default access specifier for class is "private" and for struct is "public". Secondly, you wouldn't be able to access item01's variables within BaseClass, because BaseClass does not hold an instance of item01. It only holds the class data itself. Unless BaseClass has its own private instance of item01, it cannot change anything, even if item01 is in the class.

The item01 data members themselves, like var01 and var02 are PUBLIC. If you have a item01 instance, any code can modify var01 and var02 outside of class methods.
Last edited on May 17, 2020 at 7:22pm
May 17, 2020 at 11:15pm
1
2
3
4
5
6
7
8
9
10
11
12
13
template<typename T, size_t dimensions, size_t... some_additional_args>
struct Grid
{
    Grid() = delete; //Cannot make a Grid<T, size_t, size_t...> object.
    template<size_t FirstSize, size_t... Sizes>
    struct DimensionSize///...but you can make a Grid::DimensionSize object.
    {
        std::vector<typename Grid<T, dimensions-1>::template DimensionSize<Sizes...>> vec{FirstSize};
    };
};

typename Grid<int,3, 1,2,3,4,5 /*some random args for internal stuff*/>
    ::template DimensionSize<10, 20, 5> myGrid;



How do you print the contents of myGrid?


No, the class item01 ITSELF is private within the context of SubClass, because the default access specifier for class is "private" and for struct is "public". Secondly, you wouldn't be able to access item01's variables within BaseClass, because BaseClass does not hold an instance of item01. It only holds the class data itself. Unless BaseClass has its own private instance of item01, it cannot change anything, even if item01 is in the class.



How do you access item01?

bc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class a {
public:
	class sub {
		struct item {
			
			int var1, var2;
			item()
			: var1(var1), var2(var2) {}
		};
		item bb;
	};
	sub cc;
};

	
int main() {

	item bbbb; //error

	a::sub::item bbb; //error
}

May 17, 2020 at 11:33pm
How do you print the contents of myGrid?


You would need additional member functions that provide direct access to the underlying vector. This could be, perhaps, an overloaded operator[] that returns the specified element in the vector. Then you would be able to use the Grid template as if it were just an array and be able to print it out in a for loop or something. I didn't implement such functions because it was irrelevent to the example I was trying to show.

How do you access item01?


In the example you gave:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class a {
public:
	class sub {
        public: //Make sure the item class is public and visible to clients
		struct item {
			
			int var1, var2;
			item()
			: var1(var1), var2(var2) {}
		};
		//item bb; //You don't need this
	};
	//sub cc; //You don't need this.
};

	
int main() {

	a::sub::item bbbb; //Can access var1, var2
	a::sub::item bbb;   //Ditto
        a::sub s; //Cannot access var1, var2 because it is not an a::sub::item, it is only an a::sub
        a a_; //Cannot access var1, var2 because it is not an a::sub::item, it is only an a.

        //Remember, a, a::sub, and a::sub::item are COMPLETELY UNRELATED CLASSES. They have 
        //nothing in common. The only thing is that they are within each other. 
}


Last edited on May 17, 2020 at 11:50pm
May 17, 2020 at 11:50pm
No doubt but that doesn't mesh with what you said here:


No, the class item01 ITSELF is private within the context of SubClass, because the default access specifier for class is "private" and for struct is "public"..... 

The item01 data members themselves, like var01 and var02 are PUBLIC. If you have a item01 instance, any code can modify var01 and var02 outside of class methods.


Please post example of printing contents of myGrid.
Last edited on May 18, 2020 at 12:10am
May 18, 2020 at 12:37am
No doubt but that doesn't mesh with what you said here:


It does match what I said. Read what I said.

No, the class item01 ITSELF is private within the context of SubClass


OP had the following code for item01:
1
2
3
4
5
6
7
8
9
10
//...
    class SubClass
    {
        struct item01
        {
            int var01 = 1;
            int var02 = 2;
        };
    };
//... 


Since SubClass does not have a public: access specifier before listing item01, that means that it is private by default, because SubClass is a class, and a class has private access by default. However, if it had been a STRUCT SubClass:

1
2
3
4
5
6
7
8
9
10
//...
    struct SubClass
    {
        struct item01
        {
            int var01 = 1;
            int var02 = 2;
        };
    };
//... 


Then it would be PUBLIC by default, and thus you can access it outside like BaseClass::SubClass::item01.

The item01 data members themselves, like var01 and var02 are PUBLIC. If you have a item01 instance, any code can modify var01 and var02 outside of class methods.


This is also true. Because, as I mentioned, item01 is a STRUCT and structs have PUBLIC access specifiers by default.

Please post example of printing contents of myGrid.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <vector>

template<typename T, size_t dimensions, size_t... some_additional_args>
struct Grid
{
    Grid() = delete; //Cannot make a Grid<T, size_t, size_t...> object.
    template<size_t FirstSize, size_t... Sizes>
    struct DimensionSize///...but you can make a Grid::DimensionSize object.
    {
        auto& operator[](ptrdiff_t x) {return vec[x];}
        const auto& operator[](ptrdiff_t x) const {return vec[x];}
    private:
        std::vector<typename Grid<T, dimensions-1>::template DimensionSize<Sizes...>> vec{FirstSize};
    };
};

//Partial specialization for 1D grid
template<typename T, size_t... some_additional_args>
struct Grid<T, 1, some_additional_args...>
{
    template<size_t Size>
    struct DimensionSize
    {
        DimensionSize() : vec(Size) {}

        auto& operator[](ptrdiff_t x){return vec[x];}
        const auto& operator[] (ptrdiff_t x) const {return vec[x];}
    private:
        std::vector<T> vec;
    };
    
};

typename Grid<int,2, 1,2,3,4,5 /*some random args for internal stuff*/>
    ::template DimensionSize<10, 20> myGrid;

int main()
{
    for(int i = 0; i < 10; ++i)
    {
        for(int j = 0; j < 20; ++j) std::cout << (myGrid[i][j] = 4);
        std::cout << '\n';
    }
}

Last edited on May 18, 2020 at 12:47am
May 18, 2020 at 1:02am

you're not answering the question. Based on this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class BaseClass
{
public:
    class SubClass
    {
        struct item01
        {
            int var01 = 1;
            int var02 = 2;
        };
    };
};

void printIt(BaseClass& cInput)
{
    std::cout << cInput.var01;
}


int main()
{
    BaseClass::SubClass::item01 myItem;
    printIt(myItem);
    return 0
}


and what you said here...



No, the class item01 ITSELF is private within the context of SubClass, because the default access specifier for class is "private" and for struct is "public".....

The item01 data members themselves, like var01 and var02 are PUBLIC. If you have a item01 instance, any code can modify var01 and var02 outside of class methods.


how do you access item01? How do you make an instance of item01?
May 18, 2020 at 1:24am
how do you access item01? How do you make an instance of item01?


Like this

1
2
3
4
5
int main()
{
    BaseClass::SubClass::item01 myItem; //Instance of item01
    myItem.var01 = 12345; //Accessing var01 outside of the struct because it is a public member.
}


Assuming of course item01 is public and accessible, which in your case, it is not since it is private by default, as I stated before.

Not sure where this is falling apart for you.
Last edited on May 18, 2020 at 1:34am
May 18, 2020 at 2:01am
it falls apart is where you claimed that even tho item01 is private in the context of SubClass the struct item01 and the variables inside where still accessible. Which they are not. Just admit you were wrong and move on. Now it just sounds like your backtracking.

May 18, 2020 at 2:09am
it falls apart is where you claimed that even tho item01 is private in the context of SubClass the struct item01 and the variables inside


Which is exactly what I said in this comment:

You are taking a reference to a BaseClass, but you are passing in a BaseClass::SubClass::item01. Obviously that won't work. Furthermore, your item01 class is private in the SubClass (by default) so you woudn't even be able to instantiate it in line 22.


And in this comment:

No, the class item01 ITSELF is private within the context of SubClass, because the default access specifier for class is "private" and for struct is "public".


The item01 data members themselves, like var01 and var02 are PUBLIC. If you have a item01 instance, any code can modify var01 and var02 outside of class methods.


The statement was IF you had an item01 instance, you would be able to access var01 and var02 outside of class methods. However given the first statement above, you cannot DIRECTLY create an instance. Although it is still possible if SubClass had a static member function that grants access to item01 and reterns an object, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
class BaseClass
{
public:
    class SubClass
    {
        struct item01 //still private
        {
            int var01 = 1;
            int var02 = 2;
        };
        public:
            static item01 return_item(){return item01{};}
    };
};
template<typename T>
void printIt(T& cInput)
{
    std::cout << cInput.var01;
}


int main()
{
    auto myItem = BaseClass::SubClass::return_item();
    myItem.var01 = 3;
    printIt(myItem);
    return 0;
}


Or even a friend function would do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
class BaseClass
{
public:
    class SubClass
    {
        struct item01 //still private
        {
            int var01 = 1;
            int var02 = 2;
        };
        friend item01 return_item(); //Also private friend declaration.

    };
};

BaseClass::SubClass::item01 return_item()
{
    return BaseClass::SubClass::item01{};
}
template<typename T>
void printIt(T& cInput)
{
    std::cout << cInput.var01;
}


int main()
{
    auto myItem = return_item(); //Simpler for client code to access.
    myItem.var01 = 3;
    printIt(myItem);
    return 0;
}

So no, I was not wrong. That was intentional.

Then you ask the following:

Please post example of printing contents of myGrid.


Once I do so, you go completely silent and ignore it because you knew you were wrong and didn't want to embarrass yourself.

So not only are you an arrogant asshole, you also don't know what the fuck you're even saying. Judging by your previous conversation with dutch in another thread, it seems like that's more than evident. Huge superiority complex, yet with little actual substance, knowledge, nor experience on the subject.

You should just stick to asking questions in the beginners forum because it is clear you don't know what you're talking about.

Thanks for playing, though.
Last edited on May 18, 2020 at 2:52am
May 18, 2020 at 3:40am

Once I do so, you go completely silent and ignore it because you knew you were wrong and didn't want to embarrass yourself.

So not only are you an arrogant asshole, you also don't know what the fuck you're even saying. Judging by your previous conversation with dutch in another thread, it seems like that's more than evident. Huge superiority complex, yet with little actual substance, knowledge, nor experience on the subject.


silent about what i asked you a question, kudos on your answer. I was interested in how it printed bc i wanted to know how the random numbers correlated to the constructed vector. Which they were just there for show i guess. As far as the rest of your statement...

tldr but talk about the pot calling the kettle black. You got serious issues dude. Talk about arrogance i've seen you countless times act like people are dumb to be asking beginner questions in the beginners section. If you're so billy bad ass go hang out in the advanced section bc clearly the beginners section is beneath you. I mean shit if your so good why are you not working for some major developer? Clearly you should be, why are you wasting your time taking beginners programming in college or whatever. You'd think microsoft would be throwing hookers through your window by now. The truth of the matter is that your just a big fish in a small pond. Congratulations your smarter than some random people on the internet.
May 18, 2020 at 3:49am
Nice tangent to try to cover up your mistakes and play the victim.

This entire thread I was nothing but respectful towards you. I actually spent time writing the code for that Grid class template to make sure you understood the concept.

Then you try to turn around and arrogantly say that I'M the one who is wrong, and that I should admit my mistake, when you clearly had no idea what you were talking about. That's first class arrogance right there. Those who are not courteous do not deserve a level of respect and courtesy that they have not earned.

i've seen you countless times act like people are dumb to be asking beginner questions in the beginners section.


In all my responses, I am usually very civil and respectful, as I have been here, so long as the questioner demonstrates the same courtesy back to me.
Last edited on May 18, 2020 at 3:49am
May 18, 2020 at 4:11am
lol ok dude. You say im wrong, i take my licks and move on. I say your wrong you take it as a personal attack and act like a petulant child. Its literally the definition of arrogance. I never claimed to be the c++ master. Hence look around you. I answer questions to the best of my ability. If i'm wrong i'm wrong i don't really give a hoot. This is just a hobby for me. If accusing someone of being wrong is disrespectful then you're clearly as disrespectful as they come.
Pages: 12