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 9129723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:48:49+00:00 2026-06-17T07:48:49+00:00

public struct Unit { Unit u; } Causes: Struct member ‘Unit.u’ of type ‘Unit’

  • 0
public struct Unit
{
    Unit u;
}

Causes:

Struct member ‘Unit.u’ of type ‘Unit’ causes a cycle in the struct
layout.

But

public class Unit
{
    Unit u;
}

compiles. I understand the problem I suppose. An endless cycle will be formed when referencing a Unit object since it will have to initialize another member Unit and so on. But why does the compiler restrict the problem just for structs? Doesn’t the issue persist for class too? Am I missing something?

  • 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-06-17T07:48:50+00:00Added an answer on June 17, 2026 at 7:48 am

    The problem is in terms of layout.

    When Unit is a struct, any value for a Unit would have to contain another value of the same type (and thus the same size), ad infinitum. That’s impossible. I suppose you could argue that with no other fields, the fields for Unit should take up no memory, so you could contain it within itself – but I believe the way the CLR works ensures that all structs take up at least 1 byte…

    When Unit is a class, a Unit object only has to contain a reference to another Unit object. No storage problems, and the value can be null to start with.

    Think of it this way: you can’t have a house which contains another house constructed from the same blueprint, but you can certainly have a house which contains a piece of paper with a similar house’s address on it…

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

Sidebar

Related Questions

I have a class class PCB { public: struct { string type; **linklist list;**//refer
My struct class: public struct PinAndRadius { public string pinID { get; set; }
namespace highscores { public class Game1 : Microsoft.Xna.Framework.Game { //Stuff for HighScoreData public struct
Consider the code below in a single translation unit: class C { private: struct
Take the following struct class: public struct SingleWraper { private double _myValue; public double
I have a class which contains several structs: public class NavigationMenu { public struct
public struct Parameter { public Parameter(string name, string type, string parenttype) { this.Name =
//header file class Foo { public: struct FooBar { bool ok; string msg; };
i have a structure public struct SERVER_USB_DEVICE { USB_HWID usbHWID; byte status; bool bExcludeDevice;
Code to illustrate : public struct MyStruct { public int SomeNumber; } public string

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.