C++中Triangle类构造函数传参异常及无参构造属性值一致问题求助
Let’s walk through the problems you’re facing and get your Triangle class working correctly. From what you’ve described, there are two main culprits here: incorrect constructor implementation (leading to identical objects when using parameterized construction) and uninitialized member variables from the default no-arg constructor.
1. Why Your Parameterized Constructor Creates Identical Objects
The most common reasons for this behavior are:
- Name collision between parameters and member variables: If your constructor parameters share the same name as your class members (like using
m_IDas a parameter), assigningm_ID = m_IDjust sets the variable to itself, completely ignoring the input value. - Accidentally marking member variables as
static: Static members are shared across all instances of the class, so everyTriangleobject would inherit the same values for those variables.
Fix for Parameterized Constructor
First, confirm none of your member variables are static (unless you intentionally want shared state across all objects). Then, use either initialization lists (the most efficient and clean approach) or this-> to resolve name conflicts. Here’s a corrected implementation:
class Triangle { private: // Non-static members: each object gets its own unique copy int m_ID; int m_S1, m_S2, m_S3; int m_T1, m_T2, m_T3; public: // Parameterized constructor using initialization list Triangle(int id, int s1, int s2, int s3, int t1, int t2, int t3) : m_ID(id), m_S1(s1), m_S2(s2), m_S3(s3), m_T1(t1), m_T2(t2), m_T3(t3) {} // Add getters to verify object values (optional but helpful) int getID() const { return m_ID; } int getS1() const { return m_S1; } // ... repeat getters for other members };
If you must use matching names for parameters and members, use this-> to explicitly reference the class member:
Triangle(int m_ID, int m_S1, int m_S2, int m_S3, int m_T1, int m_T2, int m_T3) { this->m_ID = m_ID; this->m_S1 = m_S1; // ... assign remaining members }
When creating objects, double-check you’re passing unique values for each instance:
Triangle triangle1(1, 0, 1, 2, 3, 4, 5); Triangle triangle2(2, 1, 2, 3, 4, 5, 6); // Unique ID and vertex/neighbor values Triangle triangle3(3, 2, 3, 4, 5, 6, 7);
2. Fixing the No-Arg Constructor Issue
By default, if you don’t define any constructors, C++ generates a default no-arg constructor—but this constructor does not initialize built-in types like int. This leaves your member variables with random garbage values, and if you’re seeing identical values across objects, it’s just coincidental memory garbage, not intentional behavior.
Options for the No-Arg Constructor
- Explicitly initialize all members: If you need to create empty
Triangleobjects, define a no-arg constructor that sets sensible default values:Triangle() : m_ID(-1), // Use -1 to signal an uninitialized/empty triangle m_S1(-1), m_S2(-1), m_S3(-1), m_T1(-1), m_T2(-1), m_T3(-1) {} - Delete the no-arg constructor: If you never need to create
Triangleobjects without parameters, delete the default constructor to prevent accidental uninitialized objects:Triangle() = delete;
Final Quick Checks
- Double-check that none of your member variables are marked
staticunless you specifically want shared state across allTriangleinstances. - Use the getter methods you added to confirm each object holds the correct values after creation.
内容的提问来源于stack exchange,提问作者Michael Landry

