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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:29:12+00:00 2026-06-12T18:29:12+00:00

As I have understood, a class can be defined in multiple translation units aslong

  • 0

As I have understood, a class can be defined in multiple translation units aslong they’re identical. With that in mind, consider the following examples:

 //1.cpp

class Foo{
 public:
  int i;
};


void FooBar();

void BarFoo(){
  Foo f;
}


int main(){
 FooBar();
 BarFoo();
}

//2.cpp

class Foo{
 public:
  std::string s;
};

void FooBar(){
  Foo f;
}

This compiles and I don’t get a crash.

If I do the following changes:

//1.cpp
 Foo FooBar();
//2.cpp
 Foo FooBar(){
   Foo f;
   return f;
 }

I get a crash. Why does one result in a crash and the other doesn’t. Also, am I not violating ODR in the first example? If I am, why does it compile ok?

  • 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-12T18:29:13+00:00Added an answer on June 12, 2026 at 6:29 pm

    The program is ill-formed for the reason you stated. The compiler is not required a diagnostics, but I don’t see a point in discussing reasons for a crash in an ill-formed program.

    Still, let’s do it:

    The first example probably doesn’t crash because FooBar‘s behavior doesn’t affect the run of main. The method is called, it does something, and that’s it.

    In the second example, you attempt to return a Foo. FooBar returns the version of Foo defined in 2.cpp. main appears in 1.cpp so it expects the version of Foo defined in 1.cpp, which is a completely different version – different members, sizes. You most likely get a corruption on the destructor. (just a guess)

    EDIT: this does break the one definition rule:

    3.2 One definition rule [basic.def.odr]

    6) There can be more than one definition of a class type […] in a program provided that each definition
    appears in a different translation unit, and provided the definitions satisfy the following requirements. […]

    • each definition of D shall consist of the same sequence of tokens;

    […]

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

Sidebar

Related Questions

I have the following class written by someone else that I'm trying to understand
I could understood that Desktop applications in Java doesn't have a pre-defined relative path
i have understood that a namespace is a container of units; but i haven't
My problem is the following; I have a class that uses non-static native methods
I have a class that has a property that is defined as interface. Users
I am trying to understand Puppet parameterized classes. I have a parameterized class defined
I thought I understood sed but I guess not. I have the following two
It is said that when we have a class Point and knows how to
In order to implement auto-vivification of Ruby hash, one can employ the following class
I have not understood the following code snippet why afterDelay(0) {...} , a locally

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.