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

  • Home
  • SEARCH
  • 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 6920835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:11:58+00:00 2026-05-27T10:11:58+00:00

I have a doubt regarding copy Overview: I have 2 classes namely Car and

  • 0

I have a doubt regarding copy

Overview:

  • I have 2 classes namely Car and MutableCar
  • Both these classes conform to the protocol NSCopying
  • The method copy would return an instance of Car

Question

  1. Why doesn’t the compiler doesn’t throw any compilation error for the following statement?

    MutableCar* c2 = [c1 copy];

    The compiler allows me to assign Car* to a MutableCar* pointer variable

  2. Is there any way that this can be prevented from going unnoticed at compile time?

    IMHO this could lead to crashes at runtime as shown in the example below.

Code (in separate files)

Points to note – Automatic Reference Counting (ARC) is used

Car.h

#import<Foundation/Foundation.h>

@interface Car : NSObject <NSCopying>
@property (readonly) int n1;
@end

Car.m

#import"Car.h"
#import"MutableCar.h"

@interface Car()                //extension
@property (readwrite) int n1;
@end

@implementation Car

@synthesize n1 = _n1;

- (id) copyWithZone: (NSZone*) pZone
{
    Car* newInstance = [[Car alloc] init];
    newInstance -> _n1 = _n1;
    return(newInstance);
}
@end

MutableCar.h

#import"Car.h"

@interface MutableCar : Car 
@property int n1;            // redeclaration
@property int n2;

@end

MutableCar.m

#import"MutableCar.h"

@implementation MutableCar
@dynamic n1;
@synthesize n2 = _n2;
@end

test.m

#import"MutableCar.h"

int main()
{
    MutableCar* c1 = [[MutableCar alloc] init];
    MutableCar* c2 = [c1 copy];                     //Car* is being assigned to MutableCar* variable
                                                    //Why doesn't the compiler doesn't throw any compilation error ?


    //c2.n2 = 20;                                     //At runtime this throws an error, because c2 is not a MutableCar instance 

    return(0);
}
  • 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-27T10:11:59+00:00Added an answer on May 27, 2026 at 10:11 am

    -[NSObject copy] is declared to return id, a type is assignable to any object pointer. That’s why you don’t get an error or a warning.

    If you override copy in @interface Car, declaring it to return Car *, you’ll get a compiler warning on your bogus assignment.

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

Sidebar

Related Questions

I have a very basic doubt regarding the method that gets executed when app
I have a doubt regarding property redeclaration Overview: class A is the parent class
I have one doubt regarding casting. public void Test(out T a, out T b)
I have a doubt regarding the backlog value in listen system call. From man
I have a doubt about orkut's new URL Writing method. Earlier the home page
I have a doubt regarding struct net_device in linux kernel. Every frame in linux
I have basic doubt regarding executable stored in ROM. As I know the executable
I have one doubt regarding audio processing for noise reduction. Is there any free
I have a slight doubt regarding virtual memory. Normally, it is up to the
I have a small doubt regarding network connection procedure in Android. My scenario is

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.