c2 classes

Ali fakeemail at example.com
Fri Apr 6 14:39:42 UTC 2018


On Friday, 6 April 2018 at 13:41:50 UTC, aerto wrote:
> its possible to make this work ??
>
> import std.stdio;
>
>
> class UUsers
> {
> public:
>     int age;
> }
>
>
> class users
> {
> public:
>     int[int] uid;
>
> }
>
>
> void main() {
> 	
> 	users newuser = new users();
> 	newuser.uid[0].age = 23;
> 	writeln(newuser.uid[0].age);
>
> }

This will work
import std.stdio;

class UUsers
{
public:
     int age;

}


class users
{
public:
     UUsers[] uid;

}


void main() {

	users userList = new users();

	userList.uid ~=  new UUsers();

         userList.uid[0].age = 24 ;

	writeln(userList.uid[0].age);
}

Let me try to explain why
Basically, what you wanted to do is have two classes
one will hold user's information, and another holding a user list

For the second class to hold user list
this line
> int[int] uid;

became this line
> UUsers[] uid;

In you code, there was no relation between the two classes
Then in your main

You instantiate your users list object
> users userList = new users();

Then you instantiate a user object and append it to the list (~= 
is the append operator)

> userList.uid ~=  new UUsers();

Finally you assign a value to your user object age and print it
> userList.uid[0].age = 24 ;
> writeln(userList.uid[0].age);


More information about the Digitalmars-d-learn mailing list