Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6664269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:35:30+00:00 2026-05-26T02:35:30+00:00

I have 2 classes A and B,B is inherited from A,A is inherited from

  • 0

I have 2 classes A and B,B is inherited from A,A is inherited from NSObject.Class A has a function -(void)init:

- (void)init {
if (self = [super init]) {

NSLog(@"A");    

}
return self;
} 

B has:

- (id)init {
if (self = [super init]) {

NSLog(@"B");    

}
return self;
} 

After compilation I have “A” and “B” in my console,despite of condition in B “if (self = [super init])” can’t be true,cause its superclass init method returns void.Any ideas?

P.S. I know that init method must return id,I’m interested why this works while it shouldn’t.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:35:31+00:00Added an answer on May 26, 2026 at 2:35 am

    Listen to Chuck. He’s smart 🙂

    I’ll try to add a bit more detail.

    One way to think about this is “If both were declared -(id)init how would A return its value to B”?

    It goes something like this. The return self statement would take the pointer to A and put it in an agreed upon location. This location would be defined in the ABI. On a OSX/x86-64 machine, it’s the RAX register (IIRC). So return self really says “write the pointer to self into RAX”.

    Then when B regains control it knows the return value is in RAX and can use it however it likes. Note you didn’t remove the return self statement from A’s implementation, so self was probably still written to RAX where B found it and used it. Or maybe RAX still had a pointer from the NSObject init implementation. You’d have to disassemble it to know for sure.

    Now let’s say you didn’t keep the return self in A’s implementation. Now there’s some random junk in RAX which B is going to try to use like it’s a pointer to an A. This is where the undefined behavior Chuck mentions comes in. If you’re lucky it will just crash.

    I’m guessing your code above gave some compiler error/warning that you had to suppress?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have classes which are inherited from abstract Packet( this class has abstract method
I have three classes: public partial class Student : Contact { //Inherited from Contact:
I am learning EF.I have some classes of model and class that inherited from
I have a base class that inherits List<> which has multiple classes derived from
I have a base class that a number of other classes inherit from. The
I have defined an Event class: Event and all the following classes inherit from
I have a whole bunch of child classes that inherit from a parent class
I have a bas class called Media with two classes that inherit from it,
I have two classes, one inherited from the other. When I compile, I get
In C++, I have an Object class, from which a number of other classes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.