CARVIEW |
Select Language
HTTP/2 200
date: Sun, 27 Jul 2025 17:19:06 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
x-robots-tag: none
etag: W/"6af520ba3fbdf3841c7c72dd909dd0e6"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: no-referrer-when-downgrade
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=4BUej9p%2BooEtoILBgSZuVfy7KLdKdUeIO7hMf3cl%2BKt3Cik7rhJmjBIPZCm4u2BY3oHESJJXPJhEVmiW%2FVL9GSrkL5Rep2q3d6eWZKTIKb76jIV1Glq3Yz28AfwiD7cmxOxZhSHYuNvEdb18ixZHhrZz3A4d1lt%2BMm9b8uA%2BWNy2vV22Ft2VyLZ5GExkoBqwWwXj%2BRzCbd2kjvt5SUl%2Ba4iLijyP5d51EYGvmY2J1VCAgnRsKg2PuMLCIG9EoaBXSk8JkCIgGKlaQ47mcp%2BOVg%3D%3D--URIa00EXCNY2K7uu--fv2Uhs7%2FGt6CKq72eTU66w%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.2041248095.1753636746; Path=/; Domain=github.com; Expires=Mon, 27 Jul 2026 17:19:06 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Mon, 27 Jul 2026 17:19:06 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: 8204:3240E6:10448CC:155522F:68865F8A
Array Generics · rusthon/Rusthon Wiki · GitHub
Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 51
Array Generics
brett hartshorn edited this page Jan 22, 2015
·
2 revisions
C++ supports looping over an array of objects, where each item in the array may be a different subclass, as long as they all share a common base class, and the array is typed as the base class. This is not a problem until you want to call a method that is only defined on one of the subclasses. To workaround this problem you can use if isinstance(item,MyClass)
inside the loop to catch any items of that class, and use data members and methods unique to that subclass inside the if-block.
https://github.com/rusthon/Rusthon/blob/master/examples/array_generics.md
class A:
def __init__(self, x:int):
self.x = x
def method1(self) -> int:
return self.x
class B(A):
def method1(self) ->int:
return self.x * 2
def method2(self, y:int):
print( self.x + y )
class C(A):
def method1(self) ->int:
return self.x + 200
def say_hi(self):
print('hi from C')
def main():
a = A( 1 )
b = B( 200 )
c = C( 3000 )
arr = []A( a,b,c )
for item in arr:
print(item.method1())
if isinstance(item, B):
item.method2( 20 )
if isinstance(item, C):
item.say_hi()
c++ output
class A {
public:
std::string __class__;
int x;
void __init__(int x);
int method1();
A() {__class__ = std::string("A");}
};
void A::__init__(int x) {
this->x = x;
}
int A::method1() {
return this->x;
}
class B: public A {
public:
int method1();
void method2(int y);
B() {__class__ = std::string("B");}
};
int B::method1() {
return (this->x * 2);
}
void B::method2(int y) {
std::cout << (this->x + y) << std::endl;
}
class C: public A {
public:
int method1();
void say_hi();
C() {__class__ = std::string("C");}
};
int C::method1() {
return (this->x + 200);
}
void C::say_hi() {
std::cout << std::string("hi from C") << std::endl;
}
int main() {
A _ref_a = A{};_ref_a.__init__(1); std::shared_ptr<A> a = std::make_shared<A>(_ref_a);
B _ref_b = B{};_ref_b.__init__(200); std::shared_ptr<B> b = std::make_shared<B>(_ref_b);
C _ref_c = C{};_ref_c.__init__(3000); std::shared_ptr<C> c = std::make_shared<C>(_ref_c);
std::vector<std::shared_ptr<A>> _ref_arr = {a,b,c};std::shared_ptr<std::vector<std::shared_ptr<A>>>
arr = std::make_shared<std::vector<std::shared_ptr<A>>>(_ref_arr);
for (auto &item: _ref_arr) {
std::cout << item->method1() << std::endl;
if (item->__class__==std::string("B")) {
auto _cast_item = std::static_pointer_cast<B>(item);
_cast_item->method2(20);
}
if (item->__class__==std::string("C")) {
auto _cast_item = std::static_pointer_cast<C>(item);
_cast_item->say_hi();
}
}
return 0;
}
You can’t perform that action at this time.